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

Fix static info parsing when export data fetching method as variable #40317

Merged
merged 1 commit into from
Sep 7, 2022
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
13 changes: 13 additions & 0 deletions packages/next/build/analysis/get-page-static-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ export function checkExports(swcAST: any): { ssr: boolean; ssg: boolean } {
}
}

if (
node.type === 'ExportDeclaration' &&
node.declaration?.type === 'VariableDeclaration'
) {
const id = node.declaration?.declarations[0]?.id.value
if (['getStaticProps', 'getServerSideProps'].includes(id)) {
return {
ssg: id === 'getStaticProps',
ssr: id === 'getServerSideProps',
}
}
}

if (node.type === 'ExportNamedDeclaration') {
const values = node.specifiers.map(
(specifier: any) =>
Expand Down
12 changes: 12 additions & 0 deletions test/unit/fixtures/page-runtime/ssr-variable-gssp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function Nodejs() {
return 'nodejs'
}

// export an identifier instead of function
export const getServerSideProps = async () => {
return { props: {} }
}

export const config = {
runtime: 'experimental-edge',
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@ function createNextConfig(runtime?: 'experimental-edge' | 'nodejs') {
}
}

describe('parse page runtime config', () => {
describe('parse page static info', () => {
it('should parse nodejs runtime correctly', async () => {
const { runtime } = await getPageStaticInfo({
const { runtime, ssr, ssg } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/nodejs-ssr.js'),
nextConfig: createNextConfig(),
})
expect(runtime).toBe('nodejs')
expect(ssr).toBe(true)
expect(ssg).toBe(false)
})

it('should parse static runtime correctly', async () => {
const { runtime } = await getPageStaticInfo({
const { runtime, ssr, ssg } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/nodejs.js'),
nextConfig: createNextConfig(),
})
expect(runtime).toBe(undefined)
expect(ssr).toBe(false)
expect(ssg).toBe(false)
})

it('should parse edge runtime correctly', async () => {
Expand All @@ -41,22 +45,35 @@ describe('parse page runtime config', () => {
})
expect(runtime).toBe(undefined)
})

it('should parse ssr info with variable exported gSSP correctly', async () => {
const { ssr, ssg } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/ssr-variable-gssp.js'),
nextConfig: createNextConfig(),
})
expect(ssr).toBe(true)
expect(ssg).toBe(false)
})
})

describe('fallback to the global runtime configuration', () => {
it('should fallback when gSP is defined and exported', async () => {
const { runtime } = await getPageStaticInfo({
const { runtime, ssr, ssg } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/fallback-with-gsp.js'),
nextConfig: createNextConfig('experimental-edge'),
})
expect(runtime).toBe('experimental-edge')
expect(ssr).toBe(false)
expect(ssg).toBe(true)
})

it('should fallback when gSP is re-exported from other module', async () => {
const { runtime } = await getPageStaticInfo({
const { runtime, ssr, ssg } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/fallback-re-export-gsp.js'),
nextConfig: createNextConfig('experimental-edge'),
})
expect(runtime).toBe('experimental-edge')
expect(ssr).toBe(false)
expect(ssg).toBe(true)
})
})