跪拜 Guibai
← All articles
Frontend · Juejin·Gold Stone Plan

Three Ways to Serve PC and Mobile from a Single URL

By 做前端的娜娜子 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

A single-URL strategy consolidates SEO authority, eliminates broken shared links between devices, and cuts maintenance costs by keeping one codebase. The layered approach—server routing, framework context, and CSS media queries—covers the real-world edge case where a desktop user resizes their browser to a narrow window, which pure UA detection misses entirely.

Summary

Serving PC and mobile from one URL avoids SEO fragmentation, broken shared links, and the overhead of maintaining two separate codebases. The core pattern is straightforward: detect the device, then conditionally render the right UI. Three distinct strategies make this work, each with different trade-offs in complexity, user experience, and responsiveness.

The simplest approach parses the User-Agent string and redirects mobile users to a separate page. A more integrated method uses framework-level Context or Inject APIs in React or Vue to pass device type down to components, which then conditionally render without any page jump. The most flexible strategy ignores device type entirely and uses responsive hooks or CSS media queries to adapt layouts based on screen width, handling edge cases like a resized desktop browser window.

A production setup often layers all three: Nginx performs a fast server-side UA check to route traffic, the frontend framework handles component-level conditional rendering, and media queries provide final layout fine-tuning. The article includes complete code examples for each approach, from a simple UA detector to a full e-commerce site with React layouts that switch between a mobile bottom-tab navigation and a desktop sidebar with a three-column product grid.

Takeaways
UA-based redirects are the simplest to implement but fail when a desktop browser window is resized to a narrow width.
Framework Context (React) or provide/inject (Vue) parses the UA once and shares the device type across all components, avoiding repeated regex checks.
Responsive hooks like react-responsive or a custom Vue composable switch layouts based on actual screen width, not device identity.
Pure CSS media queries handle layout differences with zero JavaScript overhead and are ideal for spacing, columns, and font sizes.
Nginx can inspect the User-Agent header server-side and proxy mobile traffic to a different upstream service before any frontend code runs.
A combined strategy—Nginx for initial routing, Context for component-level rendering, and media queries for layout fallback—handles the widest range of scenarios.
Server-side detection returns the correct HTML to crawlers immediately, making it the right choice for SEO-sensitive pages like e-commerce product details.
Conclusions

The article's three-layer model—server routing, framework state, CSS queries—maps cleanly onto the actual failure modes of single-URL adaptation. Each layer catches what the previous one misses.

UA detection's biggest blind spot isn't spoofed headers; it's the common user behavior of resizing a desktop browser window. The combined strategy treats UA as a fast initial guess and screen width as the final authority.

The recommendation to use Context/Inject over per-component isMobile() calls is as much about testability as performance. Mocking a device type in a single provider is far simpler than mocking navigator.userAgent across dozens of unit tests.

Many teams over-invest in responsive CSS while neglecting server-side routing, but for SEO-heavy pages, the Nginx layer is the only one that guarantees crawlers see the right markup on first request.

Concepts & terms
User-Agent (UA) String
An HTTP request header that identifies the browser, operating system, and device type. Parsing it lets servers or clients guess whether the user is on a phone, tablet, or desktop.
Context / Provider Pattern (React)
A React API that makes a value available to an entire component tree without manually passing props at every level. Used here to share device type globally after a single UA check.
provide / inject (Vue 3)
Vue's dependency injection mechanism. A parent component provides data, and any descendant component can inject it, avoiding prop drilling for shared state like device type.
useMediaQuery
A React hook (from the react-responsive library) that evaluates a CSS media query and returns a boolean. It re-renders components when the viewport crosses the specified breakpoint.
Nginx Reverse Proxy
A server configuration where Nginx inspects incoming requests and forwards them to different backend services based on rules—here, routing mobile User-Agents to an H5 app server.
SSR (Server-Side Rendering)
Generating the full HTML for a page on the server rather than in the browser. Critical for SEO because search engine crawlers receive complete content immediately.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗