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(perf): exclude entire /_next/static/ dir from generated function #2753

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions src/build/templates/handler-monorepo.tmpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ export default async function (req, context) {
export const config = {
path: '/*',
preferStatic: true,
excludedPath: [
// We use `preferStatic: true` so we already won't run this on *existing* static assets,
// but by excluding this entire path we also avoid invoking the function just to 404.
'/_next/static/*',
Copy link
Contributor

@pieh pieh Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (and same thing in second template) might need to be prefixed with basePath ... or just */_next/static/* ( but that might to broad and not sure if it actually would work )

Checking how fixture with this

behave with next build && next start shows requests being made for http://localhost:3000/base/path/_next/static/chunks/webpack-039418b95632e39f.js which currently wouldn't be excluded.

],
}
5 changes: 5 additions & 0 deletions src/build/templates/handler.tmpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ export default async function handler(req, context) {
export const config = {
path: '/*',
preferStatic: true,
excludedPath: [
// We use `preferStatic: true` so we already won't run this on *existing* static assets,
// but by excluding this entire path we also avoid invoking the function just to 404.
'/_next/static/*',
],
}
26 changes: 26 additions & 0 deletions tests/e2e/simple-app.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { expect, type Locator } from '@playwright/test'
import { nextVersionSatisfies } from '../utils/next-version-helpers.mjs'
import { test } from '../utils/playwright-helpers.js'
import { join } from 'node:path'
import { readdir } from 'node:fs/promises'

const expectImageWasLoaded = async (locator: Locator) => {
expect(await locator.evaluate((img: HTMLImageElement) => img.naturalHeight)).toBeGreaterThan(0)
Expand Down Expand Up @@ -282,3 +284,27 @@ test('can require CJS module that is not bundled', async ({ simple }) => {
expect(parsedBody.notBundledCJSModule.isBundled).toEqual(false)
expect(parsedBody.bundledCJSModule.isBundled).toEqual(true)
})

test('serves a 200 for an existing static asset without invoking a function', async ({
page,
simple,
}) => {
// Since assets are hashed, we can't hardcode anything here. Find something to fetch.
const [staticAsset] = await readdir(join(simple.isolatedFixtureRoot, '.next', 'static', 'chunks'))
expect(staticAsset).toBeDefined()

const response = await page.goto(`${simple.url}/_next/static/chunks/${staticAsset}`)

expect(response?.status()).toBe(200)
expect(response?.headers()).not.toHaveProperty('x-nf-function-type')
})

test('serves a 404 for a nonexistent static asset without invoking a function', async ({
page,
simple,
}) => {
const response = await page.goto(`${simple.url}/_next/static/stale123abcdef.js`)

expect(response?.status()).toBe(404)
expect(response?.headers()).not.toHaveProperty('x-nf-function-type')
})
2 changes: 2 additions & 0 deletions tests/utils/create-e2e-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface DeployResult {
deployID: string
url: string
logs: string
isolatedFixtureRoot: string
}

type PackageManager = 'npm' | 'pnpm' | 'yarn' | 'bun' | 'berry'
Expand Down Expand Up @@ -92,6 +93,7 @@ export const createE2EFixture = async (fixture: string, config: E2EConfig = {})
cleanup: _cleanup,
deployID: result.deployID,
url: result.url,
isolatedFixtureRoot,
}
} catch (error) {
await _cleanup(true)
Expand Down
Loading