Skip to content

Commit 99aea51

Browse files
authored
enhance: cover re-exported gsp cases for runtime fallback (vercel#35326)
Cover re-exported gsp cases from vercel#35011
1 parent 2e16d02 commit 99aea51

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

packages/next/build/entries.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export async function getPageRuntime(
144144
for (const node of body) {
145145
const { type, declaration } = node
146146
if (type === 'ExportDeclaration') {
147-
// `export const config`
147+
// Match `export const config`
148148
const valueNode = declaration?.declarations?.[0]
149149
if (valueNode?.id?.value === 'config') {
150150
const props = valueNode.init.properties
@@ -155,15 +155,30 @@ export async function getPageRuntime(
155155
pageRuntime =
156156
runtime === 'edge' || runtime === 'nodejs' ? runtime : pageRuntime
157157
} else if (declaration?.type === 'FunctionDeclaration') {
158-
// `export function getStaticProps` and
159-
// `export function getServerSideProps`
158+
// Match `export function getStaticProps | getServerSideProps`
159+
const identifier = declaration.identifier?.value
160160
if (
161-
declaration.identifier?.value === 'getStaticProps' ||
162-
declaration.identifier?.value === 'getServerSideProps'
161+
identifier === 'getStaticProps' ||
162+
identifier === 'getServerSideProps'
163163
) {
164164
isRuntimeRequired = true
165165
}
166166
}
167+
} else if (type === 'ExportNamedDeclaration') {
168+
// Match `export { getStaticProps | getServerSideProps } <from '../..'>`
169+
const { specifiers } = node
170+
for (const specifier of specifiers) {
171+
const { orig } = specifier
172+
const hasDataFetchingExports =
173+
specifier.type === 'ExportSpecifier' &&
174+
orig?.type === 'Identifier' &&
175+
(orig?.value === 'getStaticProps' ||
176+
orig?.value === 'getServerSideProps')
177+
if (hasDataFetchingExports) {
178+
isRuntimeRequired = true
179+
break
180+
}
181+
}
167182
}
168183
}
169184
} catch (err) {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export { getStaticProps } from '../lib/utils'
2+
3+
export default function ImportGsp({ gsp }) {
4+
return `import-${gsp}`
5+
}

test/unit/parse-page-runtime.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,20 @@ describe('parse page runtime config', () => {
2424
)
2525
expect(runtime).toBe(undefined)
2626
})
27+
})
28+
29+
describe('fallback to the global runtime configuration', () => {
30+
it('should fallback when gSP is defined and exported', async () => {
31+
const runtime = await getPageRuntime(
32+
join(fixtureDir, 'page-runtime/fallback-with-gsp.js'),
33+
'edge'
34+
)
35+
expect(runtime).toBe('edge')
36+
})
2737

28-
it('should fallback to the global runtime configuration if a runtime is needed', async () => {
38+
it('should fallback when gSP is re-exported from other module', async () => {
2939
const runtime = await getPageRuntime(
30-
join(fixtureDir, 'page-runtime/fallback.js'),
40+
join(fixtureDir, 'page-runtime/fallback-re-export-gsp.js'),
3141
'edge'
3242
)
3343
expect(runtime).toBe('edge')

0 commit comments

Comments
 (0)