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'

demos/static-root/local-plugin/package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "local-plugin",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"preinstall": "cd ../../.. && npm i"
8+
},
9+
"author": "",
10+
"license": "ISC"
11+
}

demos/static-root/netlify.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[build]
2+
command = "next build"
3+
publish = ".next"
4+
ignore = "git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF . ../../"
5+
6+
[build.environment]
7+
# cache Cypress binary in local "node_modules" folder
8+
# so Netlify caches it
9+
CYPRESS_CACHE_FOLDER = "../node_modules/.CypressBinary"
10+
11+
[dev]
12+
framework = "#static"
13+
14+
[[plugins]]
15+
package = "./local-plugin"
16+
17+
[[plugins]]
18+
package = "@netlify/plugin-local-install-core"
19+
20+
[[context.deploy-preview.plugins]]
21+
package = "netlify-plugin-cypress"
22+
23+
[context.deploy-preview.plugins.inputs]
24+
configFile = "../../cypress/config/static-root.json"

demos/static-root/next.config.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module.exports = {
2+
// Configurable site features we support:
3+
// distDir: 'build',
4+
generateBuildId: () => 'build-id',
5+
i18n: {
6+
defaultLocale: 'en',
7+
locales: ['en', 'es', 'fr'],
8+
},
9+
async headers() {
10+
return [
11+
{
12+
source: '/',
13+
headers: [
14+
{
15+
key: 'x-custom-header',
16+
value: 'my custom header value',
17+
},
18+
],
19+
},
20+
]
21+
},
22+
trailingSlash: true,
23+
// Configurable site features _to_ support:
24+
// basePath: '/docs',
25+
// Rewrites allow you to map an incoming request path to a different destination path.
26+
async rewrites() {
27+
return {
28+
beforeFiles: [
29+
{
30+
source: '/old/:path*',
31+
destination: '/:path*',
32+
},
33+
],
34+
}
35+
},
36+
// Redirects allow you to redirect an incoming request path to a different destination path.
37+
async redirects() {
38+
return [
39+
{
40+
source: '/redirectme',
41+
destination: '/',
42+
permanent: true,
43+
},
44+
]
45+
},
46+
}

demos/static-root/pages/_app.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import '../styles/globals.css'
2+
3+
function MyApp({ Component, pageProps }) {
4+
return <Component {...pageProps} />
5+
}
6+
7+
export default MyApp

demos/static-root/pages/another.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Head from 'next/head'
2+
import { useRouter } from 'next/router'
3+
import styles from '../styles/Home.module.css'
4+
5+
export default function Home() {
6+
const { locale } = useRouter()
7+
return (
8+
<div className={styles.container}>
9+
<Head>
10+
<title>Create Next App</title>
11+
<meta name="description" content="Generated by create next app" />
12+
<link rel="icon" href="/favicon.ico" />
13+
</Head>
14+
15+
<main className={styles.main}>
16+
<h1 className={styles.title}>Another page</h1>
17+
</main>
18+
19+
20+
</div>
21+
)
22+
}

demos/static-root/pages/api/hello.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
2+
3+
export default (req, res) => {
4+
res.status(200).json({ name: 'John Doe' })
5+
}

demos/static-root/pages/index.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import Head from 'next/head'
2+
import Image from 'next/image'
3+
import { useRouter } from 'next/router'
4+
import styles from '../styles/Home.module.css'
5+
6+
export default function Home() {
7+
const { locale } = useRouter()
8+
return (
9+
<div className={styles.container}>
10+
<Head>
11+
<title>Create Next App</title>
12+
<meta name="description" content="Generated by create next app" />
13+
<link rel="icon" href="/favicon.ico" />
14+
</Head>
15+
16+
<main className={styles.main}>
17+
<h1 className={styles.title}>
18+
Welcome to <a href="https://nextjs.org">Next.js!</a>
19+
</h1>
20+
21+
<p>The current locale is {locale}</p>
22+
23+
<p className={styles.description}>
24+
Get started by editing{' '}
25+
<code className={styles.code}>pages/index.js</code>
26+
</p>
27+
28+
<div className={styles.grid}>
29+
<a href="https://nextjs.org/docs" className={styles.card}>
30+
<h2>Documentation &rarr;</h2>
31+
<p>Find in-depth information about Next.js features and API.</p>
32+
</a>
33+
34+
<a href="https://nextjs.org/learn" className={styles.card}>
35+
<h2>Learn &rarr;</h2>
36+
<p>Learn about Next.js in an interactive course with quizzes!</p>
37+
</a>
38+
39+
<a
40+
href="https://github.com/vercel/next.js/tree/master/examples"
41+
className={styles.card}
42+
>
43+
<h2>Examples &rarr;</h2>
44+
<p>Discover and deploy boilerplate example Next.js projects.</p>
45+
</a>
46+
</div>
47+
</main>
48+
49+
50+
</div>
51+
)
52+
}

demos/static-root/public/favicon.ico

14.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)