This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I18n context initial props (vercel#21930)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
86c6016
commit 52f459a
Showing
8 changed files
with
211 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
i18n: { | ||
locales: ['en', 'fr', 'de', 'it'], | ||
defaultLocale: 'en', | ||
}, | ||
} |
44 changes: 44 additions & 0 deletions
44
test/integration/i18n-support-custom-error/pages/[slug].js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useRouter } from 'next/router' | ||
|
||
const SlugPage = (props) => { | ||
const router = useRouter() | ||
|
||
return router.isFallback ? null : ( | ||
<> | ||
<div>{props.title}</div> | ||
<div id="props">{JSON.stringify(props)}</div> | ||
</> | ||
) | ||
} | ||
|
||
export const getStaticProps = async ({ locale, params }) => { | ||
if (params.slug === 'my-custom-gone-path') { | ||
return { | ||
notFound: true, | ||
} | ||
} | ||
return { | ||
props: { | ||
locale, | ||
params, | ||
title: params.slug, | ||
}, | ||
} | ||
} | ||
|
||
export const getStaticPaths = async ({ locales }) => { | ||
const mySlugs = ['my-custom-path-1', 'my-custom-path-2'] | ||
|
||
return { | ||
paths: locales.reduce( | ||
(paths, locale) => [ | ||
...paths, | ||
...mySlugs.map((slug) => ({ locale, params: { slug } })), | ||
], | ||
[] | ||
), | ||
fallback: 'blocking', | ||
} | ||
} | ||
|
||
export default SlugPage |
20 changes: 20 additions & 0 deletions
20
test/integration/i18n-support-custom-error/pages/_error.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function CustomError(props) { | ||
return ( | ||
<> | ||
<div>My Custom {props.statusCode} page</div> | ||
<div id="error-props">{JSON.stringify(props)}</div> | ||
</> | ||
) | ||
} | ||
|
||
CustomError.getInitialProps = ({ res, err, ...context }) => { | ||
// 410 - GONE | ||
if (res && context.asPath === '/my-custom-gone-path') { | ||
res.statusCode = 410 | ||
} | ||
|
||
const statusCode = res ? res.statusCode : err ? err.statusCode : 404 | ||
return { statusCode, locale: context.locale } | ||
} | ||
|
||
export default CustomError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function IndexPage() { | ||
return <p>Hi 👋</p> | ||
} |
131 changes: 131 additions & 0 deletions
131
test/integration/i18n-support-custom-error/test/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* eslint-env jest */ | ||
|
||
import { join } from 'path' | ||
import webdriver from 'next-webdriver' | ||
import { | ||
launchApp, | ||
killApp, | ||
findPort, | ||
nextBuild, | ||
nextStart, | ||
} from 'next-test-utils' | ||
|
||
jest.setTimeout(1000 * 60 * 2) | ||
|
||
const appDir = join(__dirname, '..') | ||
const locales = ['en', 'fr', 'de', 'it'] | ||
let appPort | ||
let app | ||
|
||
const runTests = () => { | ||
it('should localized [slug] routes render correctly', async () => { | ||
for (const locale of locales) { | ||
const browser = await webdriver( | ||
appPort, | ||
`${locale === 'en' ? '' : `/${locale}`}/my-custom-path-1` | ||
) | ||
|
||
expect(JSON.parse(await browser.elementByCss('#props').text())).toEqual({ | ||
locale, | ||
params: { | ||
slug: 'my-custom-path-1', | ||
}, | ||
title: 'my-custom-path-1', | ||
}) | ||
} | ||
}) | ||
|
||
it('handle custom http status maintaining locale props in custom _error page', async () => { | ||
for (const locale of locales) { | ||
const browser = await webdriver( | ||
appPort, | ||
`${locale === 'en' ? '' : `/${locale}`}/my-custom-gone-path` | ||
) | ||
|
||
expect( | ||
JSON.parse(await browser.elementByCss('#error-props').text()) | ||
).toEqual( | ||
expect.objectContaining({ | ||
locale, | ||
statusCode: 410, | ||
}) | ||
) | ||
} | ||
}) | ||
|
||
it('handle default http status maintaining locale props in custom _error page', async () => { | ||
for (const locale of locales) { | ||
const browser = await webdriver( | ||
appPort, | ||
`${locale === 'en' ? '' : `/${locale}`}/my-custom-gone-path/other-path` | ||
) | ||
|
||
expect( | ||
JSON.parse(await browser.elementByCss('#error-props').text()) | ||
).toEqual( | ||
expect.objectContaining({ | ||
locale, | ||
statusCode: 404, | ||
}) | ||
) | ||
} | ||
}) | ||
|
||
it('should work also on client side routing', async () => { | ||
for (const locale of locales) { | ||
const browser = await webdriver( | ||
appPort, | ||
`${locale === 'en' ? '' : `/${locale}`}/my-custom-path-1` | ||
) | ||
|
||
expect(JSON.parse(await browser.elementByCss('#props').text())).toEqual( | ||
expect.objectContaining({ | ||
locale, | ||
params: { slug: 'my-custom-path-1' }, | ||
title: 'my-custom-path-1', | ||
}) | ||
) | ||
|
||
await browser.eval('window.next.router.push("/my-custom-path-2")') | ||
|
||
expect(JSON.parse(await browser.elementByCss('#props').text())).toEqual( | ||
expect.objectContaining({ | ||
locale, | ||
params: { slug: 'my-custom-path-2' }, | ||
title: 'my-custom-path-2', | ||
}) | ||
) | ||
|
||
await browser.eval('window.next.router.push("/my-custom-gone-path")') | ||
|
||
expect( | ||
JSON.parse(await browser.elementByCss('#error-props').text()) | ||
).toEqual( | ||
expect.objectContaining({ | ||
locale, | ||
}) | ||
) | ||
} | ||
}) | ||
} | ||
|
||
describe('Custom routes i18n', () => { | ||
describe('dev mode', () => { | ||
beforeAll(async () => { | ||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort) | ||
}) | ||
afterAll(() => killApp(app)) | ||
runTests(true) | ||
}) | ||
|
||
describe('production mode', () => { | ||
beforeAll(async () => { | ||
await nextBuild(appDir) | ||
appPort = await findPort() | ||
app = await nextStart(appDir, appPort) | ||
}) | ||
afterAll(() => killApp(app)) | ||
runTests() | ||
}) | ||
}) |