Middleware configuration
Developer preview
Developer Preview
This page documents the developer preview of the SDK, version 20.81.1-alpha.25.sha-f076f9b857.
The middleware runs before each page request. It makes no network calls. It writes these values into the code:
- the route path
- the
keysfromrewriteRequestPath - the composition state: published, draft or editor
- the preview mode
- the default consent
- the locale and the release
- the edge mode flag
uniformMiddleware(options) returns a middleware function. handleUniformRoute({ request, ...options }) handles one request, so you can add your own logic around it.
middleware.ts
The edge middleware (vercelUniformEdgeMiddleware, uniformEdgeMiddleware) accepts the same options.
Middleware options#
| Option | Type | Description |
|---|---|---|
rewriteRequestPath | ({ url, request }) => Promise<{ path, keys? } | undefined> | Changes the path before it goes into the code. keys adds values to the page state. |
rewriteDestinationPath | ({ code, pageState, source }) => Promise<string> | Changes the rewrite destination. source is "route" or "playground". An empty string uses the default. |
queryStrings | Record<string, string[]> | The query strings to keep in the route path, for each path pattern. The middleware removes all other query strings. |
release | { id: string } | The release to show. In draft mode, the default is the releaseId query string. |
quirks | Quirks | Quirks to add. They replace the Vercel geolocation quirks that have the same name. |
defaultConsent | boolean | The default consent for this request. The default comes from the server configuration. |
locale | string | The locale for route resolution. |
Developer Preview
The options dataClient and pathPatternsWithVariations are removed. queryStrings is new. The middleware no longer reads the query strings of the project map node.
Keep query strings in the route#
By default, the middleware removes the query strings from the route path. Thus /?utm_campaign=launch and / share one cached page. To keep a query string, because the route uses it, list it for its path pattern:
middleware.ts
Each different value of a listed query string makes a different code, and thus a different cached page. List only the query strings that change the content.
Set quirks in the middleware#
middleware.ts
The quirks do not go into the code. The browser gets them in the ufqc cookie, and the edge middleware uses them to choose variants.
Add the locale to the path#
For a localized site, use rewriteRequestPath to add the default locale to a path that does not have one:
middleware.ts
For more information, refer to Localize your app.
Map URLs to project map nodes#
Use findRouteMatch to send URL patterns to a project map node, and give the dynamic segments as keys:
middleware.ts
The keys go into the code. Components read them in context.pageState.keys. Each different value of a key makes a different code, so add only the keys that the page uses.
findRouteMatch supports :name segments only. A :name segment matches one path segment.
Move the composition route#
To put the composition route in another folder, for example under a locale segment, return the new path from rewriteDestinationPath:
middleware.ts
Then put the page in app/[locale]/uniform/[code]/page.tsx.
Limit the middleware to some paths#
Use the matcher of the middleware configuration:
Set the default consent for each request#
middleware.ts
The default consent goes into the code. Thus each consent value makes a different cached page. If you prebuild pages, list each value in createUniformStaticParams. Refer to Static generation (ISR).
Show a release#
middleware.ts
In draft mode, the middleware also reads the releaseId query string. Canvas adds it when you preview a release.