-
Notifications
You must be signed in to change notification settings - Fork 27.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
114 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react' | ||
|
||
export function Page({ page }) { | ||
return <p data-page={page}>{page}</p> | ||
} | ||
|
||
export function createGetServerSideProps(page: string) { | ||
return async function getServerSideProps(ctx) { | ||
const output = ctx.req.headers['x-invoke-output'] ?? null | ||
return { props: { page, output } } | ||
} | ||
} |
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,83 @@ | ||
import { createNextDescribe } from 'e2e-utils' | ||
|
||
const { i18n } = require('./next.config') | ||
|
||
const pages = [ | ||
{ url: '/about', page: '/about', params: null }, | ||
{ url: '/blog/about', page: '/[slug]/about', params: { slug: 'blog' } }, | ||
] | ||
|
||
function checkDataRoute(data: any, page: string) { | ||
expect(data).toHaveProperty('pageProps') | ||
expect(data.pageProps).toHaveProperty('page', page) | ||
expect(data.pageProps).toHaveProperty('output', page) | ||
} | ||
|
||
createNextDescribe( | ||
'i18n-data-route', | ||
{ | ||
files: __dirname, | ||
}, | ||
({ next }) => { | ||
describe('with locale prefix', () => { | ||
describe.each(i18n.locales)('/%s', (locale) => { | ||
const prefixed = pages.map((page) => ({ | ||
...page, | ||
url: `/${locale}${page.url}`, | ||
})) | ||
|
||
it.each(prefixed)( | ||
'should render $page via $url', | ||
async ({ url, page }) => { | ||
const $ = await next.render$(url) | ||
expect($('[data-page]').data('page')).toBe(page) | ||
} | ||
) | ||
|
||
it.each(prefixed)( | ||
'should serve data for $page', | ||
async ({ url, page, params }) => { | ||
url = `/_next/data/${next.buildId}${url}.json` | ||
if (params) { | ||
const query = new URLSearchParams(params) | ||
// Ensure the query is sorted so it's deterministic. | ||
query.sort() | ||
url += `?${query.toString()}` | ||
} | ||
|
||
const res = await next.fetch(url) | ||
expect(res.status).toBe(200) | ||
expect(res.headers.get('content-type')).toBe('application/json') | ||
const data = await res.json() | ||
checkDataRoute(data, page) | ||
} | ||
) | ||
}) | ||
}) | ||
|
||
describe('without locale prefix', () => { | ||
it.each(pages)('should render $page via $url', async ({ url, page }) => { | ||
const $ = await next.render$(url) | ||
expect($('[data-page]').data('page')).toBe(page) | ||
}) | ||
|
||
it.each(pages)( | ||
'should serve data for $page', | ||
async ({ url, page, params }) => { | ||
url = `/_next/data/${next.buildId}/${i18n.defaultLocale}${url}.json` | ||
if (params) { | ||
const query = new URLSearchParams(params) | ||
// Ensure the query is sorted so it's deterministic. | ||
query.sort() | ||
url += `?${query.toString()}` | ||
} | ||
const res = await next.fetch(url) | ||
expect(res.status).toBe(200) | ||
expect(res.headers.get('content-type')).toBe('application/json') | ||
const data = await res.json() | ||
checkDataRoute(data, page) | ||
} | ||
) | ||
}) | ||
} | ||
) |
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,9 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
module.exports = { | ||
i18n: { | ||
locales: ['en-CA', 'fr-CA'], | ||
defaultLocale: 'en-CA', | ||
}, | ||
} |
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,5 @@ | ||
import { Page, createGetServerSideProps } from '../../../components/page' | ||
|
||
export default Page | ||
|
||
export const getServerSideProps = createGetServerSideProps('/[slug]/about') |
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,5 @@ | ||
import { Page, createGetServerSideProps } from '../../components/page' | ||
|
||
export default Page | ||
|
||
export const getServerSideProps = createGetServerSideProps('/about') |