Skip to content

Commit aaf12cc

Browse files
authored
feat: use in-source path over redirects for Server Handler (#122)
* feat: use in-source path over redirects * chore: add test with squirrel image
1 parent 7e73678 commit aaf12cc

File tree

6 files changed

+18
-16
lines changed

6 files changed

+18
-16
lines changed

src/build/config.ts

-11
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,6 @@ export const setPreBuildConfig = () => {
3030
process.env.NEXT_PRIVATE_STANDALONE = 'true'
3131
}
3232

33-
export const setPostBuildConfig = ({
34-
netlifyConfig,
35-
}: Pick<NetlifyPluginOptions, 'netlifyConfig'>) => {
36-
netlifyConfig.redirects ||= []
37-
netlifyConfig.redirects.push({
38-
from: '/*',
39-
to: `/.netlify/functions/${SERVER_HANDLER_NAME}`,
40-
status: 200,
41-
})
42-
}
43-
4433
export const verifyBuildConfig = ({
4534
constants: { PUBLISH_DIR },
4635
utils: {

src/build/functions/server.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ const writePackageMetadata = async () => {
5252
const writeHandlerFile = async () => {
5353
await writeFile(
5454
resolve(SERVER_HANDLER_DIR, `${SERVER_HANDLER_NAME}.js`),
55-
`import handler from './dist/run/handlers/server.js';export default handler`,
55+
`
56+
import handler from './dist/run/handlers/server.js';
57+
export default handler;
58+
export const config = {
59+
path: '/*',
60+
preferStatic: true
61+
};
62+
`,
5663
)
5764
}
5865

src/index.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { NetlifyPluginOptions } from '@netlify/build'
22
import { restoreBuildCache, saveBuildCache } from './build/cache.js'
3-
import { setPostBuildConfig, setPreBuildConfig, verifyBuildConfig } from './build/config.js'
3+
import { setPreBuildConfig, verifyBuildConfig } from './build/config.js'
44
import { copyFetchContent, copyPrerenderedContent } from './build/content/prerendered.js'
55
import {
66
copyStaticAssets,
@@ -30,8 +30,7 @@ export const onBuild = async ({ constants, utils }: NetlifyPluginOptions) => {
3030
])
3131
}
3232

33-
export const onPostBuild = async ({ constants, utils, netlifyConfig }: NetlifyPluginOptions) => {
34-
setPostBuildConfig({ netlifyConfig })
33+
export const onPostBuild = async ({ constants, utils }: NetlifyPluginOptions) => {
3534
await publishStaticDir({ constants, utils })
3635
}
3736

tests/e2e/simple-app.test.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test } from '@playwright/test'
1+
import { type Locator, expect, test } from '@playwright/test'
22
import { createE2EFixture } from '../utils/create-e2e-fixture.js'
33

44
let ctx: Awaited<ReturnType<typeof createE2EFixture>>
@@ -11,13 +11,19 @@ test.afterAll(async ({}, testInfo) => {
1111
await ctx?.cleanup?.(!!testInfo.errors.length)
1212
})
1313

14+
const expectImageWasLoaded = async (locator: Locator) => {
15+
expect(await locator.evaluate((img: HTMLImageElement) => img.naturalHeight)).toBeGreaterThan(0)
16+
}
17+
1418
test('Renders the Home page correctly', async ({ page }) => {
1519
await page.goto(ctx.url)
1620

1721
await expect(page).toHaveTitle('Simple Next App')
1822

1923
const h1 = page.locator('h1')
2024
await expect(h1).toHaveText('Home')
25+
26+
await expectImageWasLoaded(page.locator('img'))
2127
})
2228

2329
test('Serves a static image correctly', async ({ page }) => {

tests/fixtures/simple-next-app/app/page.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export default function Home() {
22
return (
33
<main>
44
<h1>Home</h1>
5+
<img src="/squirrel.jpg" alt="a cute squirrel" width="300px" />
56
</main>
67
)
78
}
Loading

0 commit comments

Comments
 (0)