Skip to content

Commit 964637b

Browse files
authored
fix: move locale detection to netlify redirects (#861)
* fix: move locale detection to netlify redirects * chore: add e2e test * chore: update tests * chore: fix types * fix: add null check * chore: fix tests Release-As: 4.0.0-beta.13
1 parent adea889 commit 964637b

File tree

28 files changed

+725
-178
lines changed

28 files changed

+725
-178
lines changed

cypress/config/static-root.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"baseUrl": "http://localhost:3000",
3+
"integrationFolder": "../../cypress/integration/static-root",
4+
"pluginsFile": "../../cypress/plugins",
5+
"screenshotsFolder": "../../cypress/screenshots",
6+
"supportFile": "../../cypress/support/index.js",
7+
"videoFolder": "../../cypress/videos"
8+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
describe('Localization', () => {
2+
it('should use sub routing to determine current locale', () => {
3+
cy.visit('/')
4+
5+
cy.findByText('The current locale is en')
6+
7+
cy.visit('/fr')
8+
cy.findByText('The current locale is fr')
9+
})
10+
11+
it('should use the NEXT_LOCALE cookie to determine the default locale', () => {
12+
cy.setCookie('NEXT_LOCALE', 'fr')
13+
cy.visit('/')
14+
15+
cy.url().should('eq', `${Cypress.config().baseUrl}/fr/`)
16+
cy.findByText('The current locale is fr')
17+
})
18+
19+
it('should use the nf_lang cookie to determine the default locale', () => {
20+
cy.setCookie('nf_lang', 'fr')
21+
cy.visit('/')
22+
23+
cy.url().should('eq', `${Cypress.config().baseUrl}/fr/`)
24+
cy.findByText('The current locale is fr')
25+
})
26+
27+
it('should use Accept-Language to choose a locale', () => {
28+
cy.visit('/', {
29+
headers: {
30+
'Accept-Language': 'fr-FR,fr;q=0.5',
31+
},
32+
})
33+
cy.url().should('eq', `${Cypress.config().baseUrl}/fr/`)
34+
cy.findByText('The current locale is fr')
35+
})
36+
37+
it('should use the NEXT_LOCALE cookie over Accept-Language header to determine the default locale', () => {
38+
cy.setCookie('NEXT_LOCALE', 'en')
39+
cy.visit({
40+
url: '/',
41+
headers: {
42+
'Accept-Language': 'fr-FR,fr;q=0.5',
43+
},
44+
})
45+
cy.url().should('eq', `${Cypress.config().baseUrl}/`)
46+
cy.findByText('The current locale is en')
47+
})
48+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
describe('Rewrites and Redirects', () => {
2+
it('rewrites: points /old to /', () => {
3+
// preview mode is off by default
4+
cy.visit('/old/another/')
5+
cy.findByText('Another page')
6+
cy.url().should('eq', `${Cypress.config().baseUrl}/old/another/`)
7+
})
8+
9+
it('redirects: redirects /redirectme to /', () => {
10+
cy.visit('/redirectme')
11+
cy.url().should('eq', `${Cypress.config().baseUrl}/`)
12+
})
13+
})

demos/default/netlify.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[build]
22
command = "next build"
33
publish = ".next"
4-
ignore = "git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF . ../"
4+
ignore = "git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF . ../../"
55

66
[build.environment]
77
# cache Cypress binary in local "node_modules" folder

demos/nx-next-monorepo-demo/netlify.toml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
[build]
2-
command = "npm run build"
3-
publish = "dist/apps/demo-monorepo/.next"
2+
command = "npm run build"
3+
publish = "dist/apps/demo-monorepo/.next"
4+
ignore = "git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF . ../../"
5+
46
[dev]
5-
command = "npm run start"
6-
targetPort = 4200
7-
7+
command = "npm run start"
8+
targetPort = 4200
9+
810
[[plugins]]
9-
package = "./local-plugin"
11+
package = "./local-plugin"
1012

1113
[build.environment]
1214
# cache Cypress binary in local "node_modules" folder

demos/static-root/.eslintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "next",
3+
"root": true
4+
}

demos/static-root/.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
27+
# local env files
28+
.env.local
29+
.env.development.local
30+
.env.test.local
31+
.env.production.local
32+
33+
# vercel
34+
.vercel

demos/static-root/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
2+
3+
## Getting Started
4+
5+
First, run the development server:
6+
7+
```bash
8+
npm run dev
9+
# or
10+
yarn dev
11+
```
12+
13+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
14+
15+
You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file.
16+
17+
[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.js`.
18+
19+
The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.
20+
21+
## Learn More
22+
23+
To learn more about Next.js, take a look at the following resources:
24+
25+
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
26+
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
27+
28+
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
29+
30+
## Deploy on Vercel
31+
32+
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
33+
34+
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../../../lib')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
name: '@netlify/plugin-nextjs-local'

0 commit comments

Comments
 (0)