Skip to content

fix: pass only first language to next-server to match platform redirects support #2138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 30, 2023

Conversation

pieh
Copy link
Contributor

@pieh pieh commented May 25, 2023

Description

Netlify locale redirects match only on first language listed in Accept-Language header. next-server handles all of them.
https://docs.netlify.com/routing/redirects/redirect-options/#redirect-by-country-or-language

Language-based redirects always match against the first language reported by the browser in the Accept-Language header regardless of quality value weighting.

This means some requests might skip Netlify language redirects (when Automatic Locale Detection in next.js is not disabled) and hit lambda that could produce locale redirect. For SSR this might not a problem, but this is a problem for ODB, because that locale redirect gets cached for everyone.

This PR basically tricks next-server to only handle first language (to match platform capability) by removing all languages except of first one in Accept-Language header before passing it to next-server

Documentation

Tests

Added cypress test, but can be also verified manually like this:

Examine status code / response headers of requests to / with __prerender_bypass cookie (/ is using ODB, so we want to ensure we execute it) with specifically crafter Accept-Language where first language on the list is not in i18n config, but there is a language in the list overall that is in i18n config - for example Accept-Language: pl, fr-CH;q=0.9, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5 (pl not in i18n, fr is in i18n)

main branch

Curl command:

curl --head --url https://netlify-plugin-nextjs-demo.netlify.app/ --header 'Accept-Language: pl, fr-CH;q=0.9, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5' --header 'cookie: __prerender_bypass=1' --header 'x-next-debug-logging: 1' --header 'x-nf-debug-logging: 1' --cookie __prerender_bypass=1

Result (truncated for readability):

HTTP/2 307
location: /fr/

Above is the problem because this would be cached for everyone if request like that happens before ODB cache is hydrated or after it expired. Note - 307 is redirect from next-server, our are 301s (separate discussion if those should be 301 or 302)

Preview deploy for this PR:

Curl command:

curl --head --url https://deploy-preview-2138--netlify-plugin-nextjs-demo.netlify.app/ --header 'Accept-Language: pl, fr-CH;q=0.9, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5' --header 'cookie: __prerender_bypass=1' --header 'x-next-debug-logging: 1' --header 'x-nf-debug-logging: 1' --cookie __prerender_bypass=1

Result:

HTTP/2 200

Relevant links (GitHub issues, etc.) or a picture of cute animal

@netlify
Copy link

netlify bot commented May 25, 2023

Deploy Preview for netlify-plugin-nextjs-nx-monorepo-demo ready!

Name Link
🔨 Latest commit 460a556
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/6476069364121800089a82d0
😎 Deploy Preview https://deploy-preview-2138--netlify-plugin-nextjs-nx-monorepo-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 25, 2023

Deploy Preview for netlify-plugin-nextjs-static-root-demo ready!

Name Link
🔨 Latest commit 460a556
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/64760693b89a8c000825aa2a
😎 Deploy Preview https://deploy-preview-2138--netlify-plugin-nextjs-static-root-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 25, 2023

Deploy Preview for next-plugin-edge-middleware ready!

Name Link
🔨 Latest commit 460a556
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-edge-middleware/deploys/64760693e796c10008fa60a6
😎 Deploy Preview https://deploy-preview-2138--next-plugin-edge-middleware.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions github-actions bot added the type: bug code to address defects in shipped code label May 25, 2023
@netlify
Copy link

netlify bot commented May 25, 2023

Deploy Preview for nextjs-plugin-custom-routes-demo ready!

Name Link
🔨 Latest commit 460a556
🔍 Latest deploy log https://app.netlify.com/sites/nextjs-plugin-custom-routes-demo/deploys/6476069364121800089a82d5
😎 Deploy Preview https://deploy-preview-2138--nextjs-plugin-custom-routes-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 25, 2023

Deploy Preview for netlify-plugin-nextjs-demo ready!

Name Link
🔨 Latest commit 460a556
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/647606935961c200081a6247
😎 Deploy Preview https://deploy-preview-2138--netlify-plugin-nextjs-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 25, 2023

Deploy Preview for netlify-plugin-nextjs-next-auth-demo ready!

Name Link
🔨 Latest commit 460a556
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-next-auth-demo/deploys/647606932ef14d0008c0824e
😎 Deploy Preview https://deploy-preview-2138--netlify-plugin-nextjs-next-auth-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 25, 2023

Deploy Preview for next-i18next-demo ready!

Name Link
🔨 Latest commit 460a556
🔍 Latest deploy log https://app.netlify.com/sites/next-i18next-demo/deploys/6476069323d7f70007f8643e
😎 Deploy Preview https://deploy-preview-2138--next-i18next-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 25, 2023

Deploy Preview for next-plugin-canary ready!

Name Link
🔨 Latest commit 460a556
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-canary/deploys/6476069352129a0008956377
😎 Deploy Preview https://deploy-preview-2138--next-plugin-canary.netlify.app/
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 25, 2023

Deploy Preview for netlify-plugin-nextjs-export-demo ready!

Name Link
🔨 Latest commit 460a556
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/64760693d67d4e000873b531
😎 Deploy Preview https://deploy-preview-2138--netlify-plugin-nextjs-export-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

// https://docs.netlify.com/routing/redirects/redirect-options/#redirect-by-country-or-language
// > Language-based redirects always match against the first language reported by the browser in the Accept-Language header regardless of quality value weighting.
// If we wouldn't keep just first language, it's possible for `next-server` to generate locale redirect that could be cached by ODB
event.headers['accept-language'] = event.headers['accept-language'].replace(/\s*,.*$/, '')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this should be done just for odb mode, and let ssr keep all languages, but it feels weird to change behavior between 2 of them?

Copy link
Contributor

@orinokai orinokai May 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, but worse, if we change it for SSR too it's going to be a breaking change because it will stop Next.js handling non-primary locales, meaning users with secondary/tertiary languages in their accept-language header will no longer see content in their language on SSR pages 😬

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having different behaviour between SSR/ODB feels wrong, and I guess it's because what we're doing is actually trying to workaround our lack of support by imposing the same limitation on Next, which would be a regression.

Copy link
Contributor Author

@pieh pieh May 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, to me it's picking one of 2 bad options and figuring it which one is worse/better. I do agree it's probably better to keep SSR pages behaving as they are (so supporting matching on all languages on the header, even if there are multiple source that can produce redirect) and limit the workaround to just ODB (it does technically make things worse to match on lowest common denominator to avoid caching wrong things, so we want to limit area we make ~worse).

In any case 4c85c21 limits the workaround just to ODB

@pieh pieh force-pushed the fix/non-first-language-locale-redirect branch from f884088 to d4ea6e6 Compare May 26, 2023 11:11
@pieh pieh marked this pull request as ready for review May 26, 2023 11:34
@pieh pieh requested a review from a team as a code owner May 26, 2023 11:34
Copy link
Contributor

@orinokai orinokai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @pieh, lgtm 🚢

@kodiakhq kodiakhq bot merged commit aca3dc6 into main May 30, 2023
@kodiakhq kodiakhq bot deleted the fix/non-first-language-locale-redirect branch May 30, 2023 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants