Skip to content
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

test: add case for marking _document with edge runtime #54316

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions test/e2e/edge-pages-support/edge-document.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { createNextDescribe } from 'e2e-utils'
import { join } from 'path'

// x-ref: https://github.com/vercel/next.js/issues/45189
createNextDescribe(
'edge render - custom _document with edge runtime',
{
files: join(__dirname, 'app'),
},
({ next }) => {
beforeAll(async () => {
await next.stop()
await next.patchFile(
'pages/_document.js',
`import Document, { Html, Head, Main, NextScript } from 'next/document'

export default class MyDocument extends Document {
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}

export const config = {
runtime: 'experimental-edge',
}
`
)
await next.start()
})
afterAll(async () => {
await next.deleteFile('pages/_document.js')
})

it('should render page properly', async () => {
const $ = await next.render$('/')
expect($('#page').text()).toBe('/index')
})
}
)
315 changes: 157 additions & 158 deletions test/e2e/edge-pages-support/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,183 +1,182 @@
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { createNextDescribe } from 'e2e-utils'
import { fetchViaHTTP, normalizeRegEx } from 'next-test-utils'
import cheerio from 'cheerio'
import { join } from 'path'
import escapeStringRegexp from 'escape-string-regexp'
import fs from 'fs-extra'

describe('edge-render-getserversideprops', () => {
let next: NextInstance
createNextDescribe(
'edge-render-getserversideprops',
{
files: join(__dirname, 'app'),
},
({ next }) => {
if ((global as any).isNextStart) {
it('should not output trace files for edge routes', async () => {
expect(await fs.pathExists(join(next.testDir, '.next/pages'))).toBe(
false
)
expect(
await fs.pathExists(join(next.testDir, '.next/server/pages/[id].js'))
).toBe(true)
expect(
await fs.pathExists(
join(next.testDir, '.next/server/pages/[id].js.nft.json')
)
).toBe(false)
expect(
await fs.pathExists(join(next.testDir, '.next/server/pages/index.js'))
).toBe(true)
expect(
await fs.pathExists(
join(next.testDir, '.next/server/pages/index.js.nft.json')
)
).toBe(false)
})
}

beforeAll(async () => {
next = await createNext({
files: new FileRef(join(__dirname, 'app')),
dependencies: {},
it('should have correct query for pages/api', async () => {
const res = await fetchViaHTTP(next.url, '/api/hello', { a: 'b' })
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
hello: 'world',
query: {
a: 'b',
},
})
})
})
afterAll(() => next.destroy())

if ((global as any).isNextStart) {
it('should not output trace files for edge routes', async () => {
expect(await fs.pathExists(join(next.testDir, '.next/pages'))).toBe(false)
expect(
await fs.pathExists(join(next.testDir, '.next/server/pages/[id].js'))
).toBe(true)
expect(
await fs.pathExists(
join(next.testDir, '.next/server/pages/[id].js.nft.json')
)
).toBe(false)
expect(
await fs.pathExists(join(next.testDir, '.next/server/pages/index.js'))
).toBe(true)
expect(
await fs.pathExists(
join(next.testDir, '.next/server/pages/index.js.nft.json')
)
).toBe(false)
it('should have correct query for pages/api dynamic', async () => {
const res = await fetchViaHTTP(next.url, '/api/id-1', { a: 'b' })
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
hello: 'again',
query: {
a: 'b',
id: 'id-1',
},
})
})
}

it('should have correct query for pages/api', async () => {
const res = await fetchViaHTTP(next.url, '/api/hello', { a: 'b' })
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
hello: 'world',
query: {
a: 'b',
},
it('should have correct query/params on index', async () => {
const res = await fetchViaHTTP(next.url, '/')
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
expect($('#page').text()).toBe('/index')
const props = JSON.parse($('#props').text())
expect(props.query).toEqual({})
expect(props.params).toBe(null)
expect(props.url).toBe('/')
})
})

it('should have correct query for pages/api dynamic', async () => {
const res = await fetchViaHTTP(next.url, '/api/id-1', { a: 'b' })
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
hello: 'again',
query: {
a: 'b',
id: 'id-1',
},
it('should have correct query/params on /[id]', async () => {
const res = await fetchViaHTTP(next.url, '/123', { hello: 'world' })
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
expect($('#page').text()).toBe('/[id]')
const props = JSON.parse($('#props').text())
expect(props.query).toEqual({ id: '123', hello: 'world' })
expect(props.params).toEqual({ id: '123' })
expect(props.url).toBe('/123?hello=world')
})
})

it('should have correct query/params on index', async () => {
const res = await fetchViaHTTP(next.url, '/')
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
expect($('#page').text()).toBe('/index')
const props = JSON.parse($('#props').text())
expect(props.query).toEqual({})
expect(props.params).toBe(null)
expect(props.url).toBe('/')
})

it('should have correct query/params on /[id]', async () => {
const res = await fetchViaHTTP(next.url, '/123', { hello: 'world' })
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
expect($('#page').text()).toBe('/[id]')
const props = JSON.parse($('#props').text())
expect(props.query).toEqual({ id: '123', hello: 'world' })
expect(props.params).toEqual({ id: '123' })
expect(props.url).toBe('/123?hello=world')
})

it('should have correct query/params on rewrite', async () => {
const res = await fetchViaHTTP(next.url, '/rewrite-me', {
hello: 'world',
it('should have correct query/params on rewrite', async () => {
const res = await fetchViaHTTP(next.url, '/rewrite-me', {
hello: 'world',
})
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
expect($('#page').text()).toBe('/index')
const props = JSON.parse($('#props').text())
expect(props.query).toEqual({ hello: 'world' })
expect(props.params).toEqual(null)
expect(props.url).toBe('/rewrite-me?hello=world')
})
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
expect($('#page').text()).toBe('/index')
const props = JSON.parse($('#props').text())
expect(props.query).toEqual({ hello: 'world' })
expect(props.params).toEqual(null)
expect(props.url).toBe('/rewrite-me?hello=world')
})

it('should have correct query/params on dynamic rewrite', async () => {
const res = await fetchViaHTTP(next.url, '/rewrite-me-dynamic', {
hello: 'world',
it('should have correct query/params on dynamic rewrite', async () => {
const res = await fetchViaHTTP(next.url, '/rewrite-me-dynamic', {
hello: 'world',
})
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
expect($('#page').text()).toBe('/[id]')
const props = JSON.parse($('#props').text())
expect(props.query).toEqual({ id: 'first', hello: 'world' })
expect(props.params).toEqual({ id: 'first' })
expect(props.url).toBe('/rewrite-me-dynamic?hello=world')
})
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
expect($('#page').text()).toBe('/[id]')
const props = JSON.parse($('#props').text())
expect(props.query).toEqual({ id: 'first', hello: 'world' })
expect(props.params).toEqual({ id: 'first' })
expect(props.url).toBe('/rewrite-me-dynamic?hello=world')
})

it('should respond to _next/data for index correctly', async () => {
const res = await fetchViaHTTP(
next.url,
`/_next/data/${next.buildId}/index.json`,
undefined,
{
headers: {
'x-nextjs-data': '1',
},
}
)
expect(res.status).toBe(200)
const { pageProps: props } = await res.json()
expect(props.query).toEqual({})
expect(props.params).toBe(null)
})

it('should respond to _next/data for [id] correctly', async () => {
const res = await fetchViaHTTP(
next.url,
`/_next/data/${next.buildId}/321.json`,
{ hello: 'world' },
{
headers: {
'x-nextjs-data': '1',
},
}
)
expect(res.status).toBe(200)
const { pageProps: props } = await res.json()
expect(props.query).toEqual({ id: '321', hello: 'world' })
expect(props.params).toEqual({ id: '321' })
})

if ((global as any).isNextStart) {
it('should have data routes in routes-manifest', async () => {
const manifest = JSON.parse(
await next.readFile('.next/routes-manifest.json')
it('should respond to _next/data for index correctly', async () => {
const res = await fetchViaHTTP(
next.url,
`/_next/data/${next.buildId}/index.json`,
undefined,
{
headers: {
'x-nextjs-data': '1',
},
}
)
expect(res.status).toBe(200)
const { pageProps: props } = await res.json()
expect(props.query).toEqual({})
expect(props.params).toBe(null)
})

for (const route of manifest.dataRoutes) {
route.dataRouteRegex = normalizeRegEx(route.dataRouteRegex)
}

expect(manifest.dataRoutes).toEqual([
it('should respond to _next/data for [id] correctly', async () => {
const res = await fetchViaHTTP(
next.url,
`/_next/data/${next.buildId}/321.json`,
{ hello: 'world' },
{
dataRouteRegex: normalizeRegEx(
`^/_next/data/${escapeStringRegexp(next.buildId)}/index.json$`
),
page: '/',
},
{
dataRouteRegex: normalizeRegEx(
`^/_next/data/${escapeStringRegexp(next.buildId)}/([^/]+?)\\.json$`
),
namedDataRouteRegex: `^/_next/data/${escapeStringRegexp(
next.buildId
)}/(?<nxtPid>[^/]+?)\\.json$`,
page: '/[id]',
routeKeys: {
nxtPid: 'nxtPid',
headers: {
'x-nextjs-data': '1',
},
},
])
}
)
expect(res.status).toBe(200)
const { pageProps: props } = await res.json()
expect(props.query).toEqual({ id: '321', hello: 'world' })
expect(props.params).toEqual({ id: '321' })
})

if ((global as any).isNextStart) {
it('should have data routes in routes-manifest', async () => {
const manifest = JSON.parse(
await next.readFile('.next/routes-manifest.json')
)

for (const route of manifest.dataRoutes) {
route.dataRouteRegex = normalizeRegEx(route.dataRouteRegex)
}

expect(manifest.dataRoutes).toEqual([
{
dataRouteRegex: normalizeRegEx(
`^/_next/data/${escapeStringRegexp(next.buildId)}/index.json$`
),
page: '/',
},
{
dataRouteRegex: normalizeRegEx(
`^/_next/data/${escapeStringRegexp(
next.buildId
)}/([^/]+?)\\.json$`
),
namedDataRouteRegex: `^/_next/data/${escapeStringRegexp(
next.buildId
)}/(?<nxtPid>[^/]+?)\\.json$`,
page: '/[id]',
routeKeys: {
nxtPid: 'nxtPid',
},
},
])
})
}
}
})
)