Skip to content

Commit 43dc28d

Browse files
committed
fix: resolve all pages using nft
1 parent 915e9dd commit 43dc28d

File tree

7 files changed

+309
-76
lines changed

7 files changed

+309
-76
lines changed

demos/default/hello.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
world
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Link from 'next/link'
2+
import path from 'path'
3+
import fs from 'fs'
4+
5+
export async function getServerSideProps() {
6+
const text = fs.readFileSync(path.join(process.cwd(), 'hello.txt'), 'utf8').trim()
7+
8+
return {
9+
props: {
10+
world: text,
11+
time: new Date().getTime(),
12+
},
13+
}
14+
}
15+
16+
const File = ({ world, time }) => (
17+
<>
18+
<p>hello {world}</p>
19+
<span id="anotherTime">time: {time}</span>
20+
<Link href="/" id="home">
21+
to home
22+
</Link>
23+
<br />
24+
<Link href="/something" id="something">
25+
to something
26+
</Link>
27+
</>
28+
)
29+
export default File

packages/runtime/src/helpers/functions.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { join, relative, resolve } from 'pathe'
1111
import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, IMAGE_FUNCTION_NAME, DEFAULT_FUNCTIONS_SRC } from '../constants'
1212
import { getApiHandler } from '../templates/getApiHandler'
1313
import { getHandler } from '../templates/getHandler'
14-
import { getPageResolver, getSinglePageResolver } from '../templates/getPageResolver'
14+
import { getResolverForPages, getResolverForSourceFiles } from '../templates/getPageResolver'
1515

1616
import { ApiConfig, ApiRouteType, extractConfigFromFile } from './analysis'
1717
import { getSourceFileForPage } from './files'
@@ -52,7 +52,7 @@ export const generateFunctions = async (
5252

5353
const resolveSourceFile = (file: string) => join(publish, 'server', file)
5454

55-
const resolverSource = await getSinglePageResolver({
55+
const resolverSource = await getResolverForSourceFiles({
5656
functionsDir,
5757
// These extra pages are always included by Next.js
5858
sourceFiles: [compiled, 'pages/_app.js', 'pages/_document.js', 'pages/_error.js'].map(resolveSourceFile),
@@ -81,17 +81,12 @@ export const generateFunctions = async (
8181
*/
8282
export const generatePagesResolver = async ({
8383
constants: { INTERNAL_FUNCTIONS_SRC, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC, PUBLISH_DIR },
84-
target,
8584
}: {
8685
constants: NetlifyPluginConstants
87-
target: string
8886
}): Promise<void> => {
8987
const functionsPath = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC
9088

91-
const jsSource = await getPageResolver({
92-
publish: PUBLISH_DIR,
93-
target,
94-
})
89+
const jsSource = await getResolverForPages(PUBLISH_DIR)
9590

9691
await writeFile(join(functionsPath, ODB_FUNCTION_NAME, 'pages.js'), jsSource)
9792
await writeFile(join(functionsPath, HANDLER_FUNCTION_NAME, 'pages.js'), jsSource)

packages/runtime/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ const plugin: NetlifyPlugin = {
155155
const apiRoutes = await getExtendedApiRouteConfigs(publish, appDir)
156156

157157
await generateFunctions(constants, appDir, apiRoutes)
158-
await generatePagesResolver({ target, constants })
158+
await generatePagesResolver({ constants })
159159

160160
await movePublicFiles({ appDir, outdir, publish })
161161

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
11
import { posix } from 'path'
22

3+
import glob from 'globby'
34
import { outdent } from 'outdent'
45
import { relative, resolve } from 'pathe'
56
import slash from 'slash'
6-
import glob from 'tiny-glob'
77

88
import { HANDLER_FUNCTION_NAME } from '../constants'
99
import { getDependenciesOfFile } from '../helpers/files'
1010

1111
// Generate a file full of require.resolve() calls for all the pages in the
1212
// build. This is used by the nft bundler to find all the pages.
1313

14-
export const getPageResolver = async ({ publish, target }: { publish: string; target: string }) => {
15-
const functionDir = posix.resolve(posix.join('.netlify', 'functions', HANDLER_FUNCTION_NAME))
16-
const root = posix.resolve(slash(publish), target === 'server' ? 'server' : 'serverless', 'pages')
14+
export const getUniqueDependencies = async (sourceFiles: Array<string>) => {
15+
const dependencies = await Promise.all(sourceFiles.map((sourceFile) => getDependenciesOfFile(sourceFile)))
16+
return [...new Set([...sourceFiles, ...dependencies.flat()])].sort()
17+
}
1718

18-
const pages = await glob('**/*.js', {
19+
export const getAllPageDependencies = async (publish: string) => {
20+
const root = posix.resolve(slash(publish), 'server')
21+
22+
const pageFiles = await glob('{pages,app}/**/*.js', {
1923
cwd: root,
24+
absolute: true,
2025
dot: true,
2126
})
22-
const pageFiles = pages
23-
.map((page) => `require.resolve('${posix.relative(functionDir, posix.join(root, slash(page)))}')`)
24-
.sort()
2527

26-
return outdent`
27-
// This file is purely to allow nft to know about these pages. It should be temporary.
28+
return getUniqueDependencies(pageFiles)
29+
}
30+
31+
export const getResolverForDependencies = ({
32+
dependencies,
33+
functionDir,
34+
}: {
35+
dependencies: string[]
36+
functionDir: string
37+
}) => {
38+
const pageFiles = dependencies.map((file) => `require.resolve('${relative(functionDir, file)}')`)
39+
return outdent/* javascript */ `
40+
// This file is purely to allow nft to know about these pages.
2841
exports.resolvePages = () => {
2942
try {
3043
${pageFiles.join('\n ')}
@@ -33,31 +46,21 @@ export const getPageResolver = async ({ publish, target }: { publish: string; ta
3346
`
3447
}
3548

36-
/**
37-
* API routes only need the dependencies for a single entrypoint, so we use the
38-
* NFT trace file to get the dependencies.
39-
*/
40-
export const getSinglePageResolver = async ({
49+
export const getResolverForPages = async (publish: string) => {
50+
const functionDir = posix.resolve(posix.join('.netlify', 'functions', HANDLER_FUNCTION_NAME))
51+
const dependencies = await getAllPageDependencies(publish)
52+
return getResolverForDependencies({ dependencies, functionDir })
53+
}
54+
55+
export const getResolverForSourceFiles = async ({
4156
functionsDir,
4257
sourceFiles,
4358
}: {
4459
functionsDir: string
4560
sourceFiles: Array<string>
4661
}) => {
47-
const dependencies = await Promise.all(sourceFiles.map((sourceFile) => getDependenciesOfFile(sourceFile)))
4862
// We don't need the actual name, just the relative path.
4963
const functionDir = resolve(functionsDir, 'functionName')
50-
51-
const deduped = [...new Set(dependencies.flat())]
52-
53-
const pageFiles = [...sourceFiles, ...deduped]
54-
.map((file) => `require.resolve('${relative(functionDir, file)}')`)
55-
.sort()
56-
57-
return outdent/* javascript */ `
58-
// This file is purely to allow nft to know about these pages.
59-
try {
60-
${pageFiles.join('\n ')}
61-
} catch {}
62-
`
64+
const dependencies = await getUniqueDependencies(sourceFiles)
65+
return getResolverForDependencies({ dependencies, functionDir })
6366
}

0 commit comments

Comments
 (0)