Skip to content

Commit

Permalink
meta: reduce middleware invocations by only running on /
Browse files Browse the repository at this point in the history
  • Loading branch information
ovflowd committed Nov 30, 2023
1 parent 74c0c5e commit 2b5336b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
15 changes: 10 additions & 5 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ export default createMiddleware({

// Used when no locale matches
defaultLocale: defaultLocale.code,

// Always use a Locale as a prefix for routing
localePrefix: 'always',

// We already have our own way of providing alternate links
// generated on `next.dynamic.mjs`
alternateLinks: false,
});

export const config = {
// Note.: This needs to be updated when activating more locales
// Format: '/(locale1|locale2|locale3|...)/:path*'
matcher: ['/', '/(en)/:path*'],
};
// We only want the middleware to run on the `/` route
// to redirect users to their preferred locale
export const config = { matcher: ['/'] };
20 changes: 13 additions & 7 deletions next.dynamic.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,22 @@ const getDynamicRouter = async () => {
: siteConfig.title;

pageMetadata.twitter.title = pageMetadata.title;
pageMetadata.alternates.canonical = `${baseUrlAndPath}/${locale}/${path}`;

pageMetadata.alternates.languages[
'x-default'
] = `${baseUrlAndPath}/${defaultLocale.code}/${path}`;
const getUrlForPathname = (l, p) =>
`${baseUrlAndPath}/${l}${p ? `/${p}` : ''}`;

pageMetadata.alternates.canonical = getUrlForPathname(locale, path);

pageMetadata.alternates.languages['x-default'] = getUrlForPathname(
defaultLocale.code,
path
);

availableLocaleCodes.forEach(currentLocale => {
pageMetadata.alternates.languages[
currentLocale
] = `${baseUrlAndPath}/${currentLocale}/${path}`;
pageMetadata.alternates.languages[currentLocale] = getUrlForPathname(
currentLocale,
path
);
});

return pageMetadata;
Expand Down

0 comments on commit 2b5336b

Please sign in to comment.