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

enhance: cover re-exported gsp cases for runtime fallback #35326

Merged
merged 3 commits into from Mar 15, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 20 additions & 5 deletions packages/next/build/entries.ts
Expand Up @@ -144,7 +144,7 @@ export async function getPageRuntime(
for (const node of body) {
const { type, declaration } = node
if (type === 'ExportDeclaration') {
// `export const config`
// Match `export const config`
const valueNode = declaration?.declarations?.[0]
if (valueNode?.id?.value === 'config') {
const props = valueNode.init.properties
Expand All @@ -155,15 +155,30 @@ export async function getPageRuntime(
pageRuntime =
runtime === 'edge' || runtime === 'nodejs' ? runtime : pageRuntime
} else if (declaration?.type === 'FunctionDeclaration') {
// `export function getStaticProps` and
// `export function getServerSideProps`
// Match `export function getStaticProps | getServerSideProps`
const identifier = declaration.identifier?.value
if (
declaration.identifier?.value === 'getStaticProps' ||
declaration.identifier?.value === 'getServerSideProps'
identifier === 'getStaticProps' ||
identifier === 'getServerSideProps'
) {
isRuntimeRequired = true
}
}
} else if (type === 'ExportNamedDeclaration') {
// Match `export { getStaticProps | getServerSideProps } <from '../..'>`
const { specifiers } = node
for (const specifier of specifiers) {
const { orig } = specifier
const hasDataFetchingExports =
specifier.type === 'ExportSpecifier' &&
orig?.type === 'Identifier' &&
(orig?.value === 'getStaticProps' ||
orig?.value === 'getServerSideProps')
if (hasDataFetchingExports) {
isRuntimeRequired = true
break
}
}
}
}
} catch (err) {}
Expand Down
5 changes: 5 additions & 0 deletions test/unit/fixtures/page-runtime/fallback-re-export-gsp.js
@@ -0,0 +1,5 @@
export { getStaticProps } from '../lib/utils'

export default function ImportGsp({ gsp }) {
return `import-${gsp}`
}
14 changes: 12 additions & 2 deletions test/unit/parse-page-runtime.test.ts
Expand Up @@ -24,10 +24,20 @@ describe('parse page runtime config', () => {
)
expect(runtime).toBe(undefined)
})
})

describe('fallback to the global runtime configuration', () => {
it('should fallback when gSP is defined and exported', async () => {
const runtime = await getPageRuntime(
join(fixtureDir, 'page-runtime/fallback-with-gsp.js'),
'edge'
)
expect(runtime).toBe('edge')
})

it('should fallback to the global runtime configuration if a runtime is needed', async () => {
it('should fallback when gSP is re-exported from other module', async () => {
const runtime = await getPageRuntime(
join(fixtureDir, 'page-runtime/fallback.js'),
join(fixtureDir, 'page-runtime/fallback-re-export-gsp.js'),
'edge'
)
expect(runtime).toBe('edge')
Expand Down