Skip to content

Commit

Permalink
Merge branch 'canary' of github.com:vercel/next.js into add/remove-ta…
Browse files Browse the repository at this point in the history
…rget
  • Loading branch information
timneutkens committed Mar 23, 2022
2 parents b97158a + 2cf6696 commit 6ae58d1
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export default async function middlewareSSRLoader(this: any) {
buildManifest,
reactLoadableManifest,
serverComponentManifest: ${isServerComponent} ? rscManifest : null,
isServerComponent: ${isServerComponent},
config: ${stringifiedConfig},
buildId: ${JSON.stringify(buildId)},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export function getRender({
buildManifest,
reactLoadableManifest,
serverComponentManifest,
isServerComponent,
config,
buildId,
}: {
Expand All @@ -39,7 +38,6 @@ export function getRender({
buildManifest: BuildManifest
reactLoadableManifest: ReactLoadableManifest
serverComponentManifest: any | null
isServerComponent: boolean
config: NextConfig
buildId: string
}) {
Expand Down Expand Up @@ -109,10 +107,6 @@ export function getRender({
const requestHandler = server.getRequestHandler()

return async function render(request: NextRequest) {
const { nextUrl: url } = request
const { searchParams } = url
const query = Object.fromEntries(searchParams)

// Preflight request
if (request.method === 'HEAD') {
// Hint the client that the matched route is a SSR page.
Expand All @@ -123,21 +117,6 @@ export function getRender({
})
}

const renderServerComponentData = isServerComponent
? query.__flight__ !== undefined
: false

const serverComponentProps =
isServerComponent && query.__props__
? JSON.parse(query.__props__)
: undefined

// Extend the render options.
server.updateRenderOpts({
renderServerComponentData,
serverComponentProps,
})

const extendedReq = new WebNextRequest(request)
const extendedRes = new WebNextResponse()
requestHandler(extendedReq, extendedRes)
Expand Down
4 changes: 4 additions & 0 deletions packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,10 @@ export default abstract class Server {
return seg
})
.join('/')

// ensure /index and / is normalized to one key
ssgCacheKey =
ssgCacheKey === '/index' && pathname === '/' ? '/' : ssgCacheKey
}

const doRender: () => Promise<ResponseCacheEntry | null> = async () => {
Expand Down
19 changes: 11 additions & 8 deletions packages/next/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,6 @@ export type RenderOptsPartial = {
resolvedUrl?: string
resolvedAsPath?: string
serverComponentManifest?: any
renderServerComponentData?: boolean
serverComponentProps?: any
distDir?: string
locale?: string
locales?: string[]
Expand Down Expand Up @@ -451,7 +449,6 @@ export async function renderToHTML(
getStaticPaths,
getServerSideProps,
serverComponentManifest,
serverComponentProps,
isDataReq,
params,
previewProps,
Expand Down Expand Up @@ -506,11 +503,17 @@ export async function renderToHTML(
return ''
}

let { renderServerComponentData } = renderOpts
if (isServerComponent && query.__flight__) {
renderServerComponentData = true
delete query.__flight__
}
let renderServerComponentData = isServerComponent
? query.__flight__ !== undefined
: false

const serverComponentProps =
isServerComponent && query.__props__
? JSON.parse(query.__props__ as string)
: undefined

delete query.__flight__
delete query.__props__

const callMiddleware = async (method: string, args: any[], props = false) => {
let results: any = props ? {} : []
Expand Down
4 changes: 0 additions & 4 deletions packages/next/server/web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,4 @@ export default class NextWebServer extends BaseServer {
components: result,
}
}

public updateRenderOpts(renderOpts: Partial<BaseServer['renderOpts']>) {
Object.assign(this.renderOpts, renderOpts)
}
}
20 changes: 11 additions & 9 deletions test/production/required-server-files-i18n.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,25 +162,27 @@ describe('should set-up next', () => {
const res2 = await fetchViaHTTP(appPort, '/gssp', undefined, {
redirect: 'manual ',
})
await next.patchFile('standalone/data.txt', 'show')

expect(res2.status).toBe(404)
expect(res2.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
)
})

it('should render SSR page correctly', async () => {
const html = await renderViaHTTP(appPort, '/')
const html = await renderViaHTTP(appPort, '/gssp')
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())

expect($('#index').text()).toBe('index page')
expect($('#gssp').text()).toBe('getServerSideProps page')
expect(data.hello).toBe('world')

const html2 = await renderViaHTTP(appPort, '/')
const html2 = await renderViaHTTP(appPort, '/gssp')
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())

expect($2('#index').text()).toBe('index page')
expect($2('#gssp').text()).toBe('getServerSideProps page')
expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random)
})
Expand Down Expand Up @@ -244,24 +246,24 @@ describe('should set-up next', () => {
it('should render SSR page correctly with x-matched-path', async () => {
const html = await renderViaHTTP(appPort, '/some-other-path', undefined, {
headers: {
'x-matched-path': '/',
'x-matched-path': '/gssp',
},
})
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())

expect($('#index').text()).toBe('index page')
expect($('#gssp').text()).toBe('getServerSideProps page')
expect(data.hello).toBe('world')

const html2 = await renderViaHTTP(appPort, '/some-other-path', undefined, {
headers: {
'x-matched-path': '/',
'x-matched-path': '/gssp',
},
})
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())

expect($2('#index').text()).toBe('index page')
expect($2('#gssp').text()).toBe('getServerSideProps page')
expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random)
})
Expand Down Expand Up @@ -508,7 +510,7 @@ describe('should set-up next', () => {
},
{
headers: {
'x-matched-path': '/',
'x-matched-path': '/gssp',
},
}
)
Expand Down
43 changes: 34 additions & 9 deletions test/production/required-server-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,29 @@ describe('should set-up next', () => {
expect(res2.status).toBe(200)
const { pageProps: props2 } = await res2.json()
expect(props2.gspCalls).toBe(props.gspCalls)

const res3 = await fetchViaHTTP(appPort, '/index', undefined, {
redirect: 'manual',
headers: {
'x-matched-path': '/index',
},
})
expect(res3.status).toBe(200)
const $2 = cheerio.load(await res3.text())
const props3 = JSON.parse($2('#props').text())
expect(props3.gspCalls).toBeDefined()

const res4 = await fetchViaHTTP(
appPort,
`/_next/data/${next.buildId}/index.json`,
undefined,
{
redirect: 'manual',
}
)
expect(res4.status).toBe(200)
const { pageProps: props4 } = await res4.json()
expect(props4.gspCalls).toBe(props3.gspCalls)
})

it('should set correct SWR headers with notFound gsp', async () => {
Expand Down Expand Up @@ -218,25 +241,27 @@ describe('should set-up next', () => {
const res2 = await fetchViaHTTP(appPort, '/gssp', undefined, {
redirect: 'manual ',
})
await next.patchFile('standalone/data.txt', 'show')

expect(res2.status).toBe(404)
expect(res2.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
)
})

it('should render SSR page correctly', async () => {
const html = await renderViaHTTP(appPort, '/')
const html = await renderViaHTTP(appPort, '/gssp')
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())

expect($('#index').text()).toBe('index page')
expect($('#gssp').text()).toBe('getServerSideProps page')
expect(data.hello).toBe('world')

const html2 = await renderViaHTTP(appPort, '/')
const html2 = await renderViaHTTP(appPort, '/gssp')
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())

expect($2('#index').text()).toBe('index page')
expect($2('#gssp').text()).toBe('getServerSideProps page')
expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random)
})
Expand Down Expand Up @@ -300,24 +325,24 @@ describe('should set-up next', () => {
it('should render SSR page correctly with x-matched-path', async () => {
const html = await renderViaHTTP(appPort, '/some-other-path', undefined, {
headers: {
'x-matched-path': '/',
'x-matched-path': '/gssp',
},
})
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())

expect($('#index').text()).toBe('index page')
expect($('#gssp').text()).toBe('getServerSideProps page')
expect(data.hello).toBe('world')

const html2 = await renderViaHTTP(appPort, '/some-other-path', undefined, {
headers: {
'x-matched-path': '/',
'x-matched-path': '/gssp',
},
})
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())

expect($2('#index').text()).toBe('index page')
expect($2('#gssp').text()).toBe('getServerSideProps page')
expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random)
})
Expand Down Expand Up @@ -564,7 +589,7 @@ describe('should set-up next', () => {
},
{
headers: {
'x-matched-path': '/',
'x-matched-path': '/gssp',
},
}
)
Expand Down
7 changes: 6 additions & 1 deletion test/production/required-server-files/pages/gssp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs'
import path from 'path'
import { useRouter } from 'next/router'

export async function getServerSideProps({ res }) {
res.setHeader('cache-control', 's-maxage=1, stale-while-revalidate')
Expand All @@ -19,16 +20,20 @@ export async function getServerSideProps({ res }) {
props: {
hello: 'world',
data,
random: Math.random(),
// make sure fetch if polyfilled
example: await fetch('https://example.com').then((res) => res.text()),
},
}
}

export default function Page(props) {
const router = useRouter()

return (
<>
<p id="gsp">getStaticProps page</p>
<p id="gssp">getServerSideProps page</p>
<p id="router">{JSON.stringify(router)}</p>
<p id="props">{JSON.stringify(props)}</p>
</>
)
Expand Down
8 changes: 7 additions & 1 deletion test/production/required-server-files/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ if (localConfig.hello !== 'world') {
throw new Error('oof import order is wrong, _app comes first')
}

export const getServerSideProps = ({ req }) => {
let gspCalls = 0

export const getStaticProps = () => {
gspCalls += 1

return {
props: {
hello: 'world',
random: Math.random(),
gspCalls,
},
revalidate: 1,
}
}

Expand Down

0 comments on commit 6ae58d1

Please sign in to comment.