Skip to content

Commit

Permalink
fix(netlify, netlify-edge): prefix build output paths with baseURL (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Jan 6, 2025
1 parent d8fb358 commit 7c780cc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
8 changes: 6 additions & 2 deletions src/presets/netlify/preset.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { promises as fsp } from "node:fs";
import { defineNitroPreset } from "nitropack/kit";
import type { Nitro } from "nitropack/types";
import { joinURL } from "ufo";
import { dirname, join } from "pathe";
import netlifyLegacyPresets from "./legacy/preset";
import {
Expand Down Expand Up @@ -85,8 +86,11 @@ const netlifyEdge = defineNitroPreset(
version: 1,
functions: [
{
path: "/*",
excludedPath: getStaticPaths(nitro.options.publicAssets),
path: `${joinURL(nitro.options.baseURL, "*")}`,
excludedPath: getStaticPaths(
nitro.options.publicAssets,
nitro.options.baseURL
),
name: "edge server handler",
function: "server",
generator: getGeneratorString(nitro),
Expand Down
20 changes: 10 additions & 10 deletions src/presets/netlify/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@ export async function writeHeaders(nitro: Nitro) {
await fsp.writeFile(headersPath, contents);
}

export function getStaticPaths(publicAssets: PublicAssetDir[]): string[] {
export function getStaticPaths(
publicAssets: PublicAssetDir[],
baseURL: string
): string[] {
return [
"/.netlify",
"/.netlify/*", // TODO: should this be also be prefixed with baseURL?
...publicAssets
.filter(
(path) =>
path.fallthrough !== true && path.baseURL && path.baseURL !== "/"
)
.map(({ baseURL }) => baseURL),
].map((url) => joinURL("/", url!, "*"));
.filter((a) => a.fallthrough !== true && a.baseURL && a.baseURL !== "/")
.map((a) => joinURL(baseURL, a.baseURL!, "*")),
];
}

// This is written to the functions directory. It just re-exports the compiled handler,
Expand All @@ -115,8 +115,8 @@ export { default } from "./main.mjs";
export const config = {
name: "server handler",
generator: "${getGeneratorString(nitro)}",
path: "/*",
excludedPath: ${JSON.stringify(getStaticPaths(nitro.options.publicAssets))},
path: "${joinURL(nitro.options.baseURL, "*")}",
excludedPath: ${JSON.stringify(getStaticPaths(nitro.options.publicAssets, nitro.options.baseURL))},
preferStatic: true,
};
`.trim();
Expand Down
8 changes: 4 additions & 4 deletions test/presets/netlify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe("nitro:preset:netlify", async () => {

describe("getStaticPaths", () => {
it("always returns `/.netlify/*`", () => {
expect(getStaticPaths([])).toEqual(["/.netlify/*"]);
expect(getStaticPaths([], "/base")).toEqual(["/.netlify/*"]);
});

it("returns a pattern with a leading slash for each non-fallthrough non-root public asset path", () => {
Expand Down Expand Up @@ -206,10 +206,10 @@ describe("nitro:preset:netlify", async () => {
maxAge: 0,
},
];
expect(getStaticPaths(publicAssets)).toEqual([
expect(getStaticPaths(publicAssets, "/base")).toEqual([
"/.netlify/*",
"/with-default-fallthrough/*",
"/nested/no-fallthrough/*",
"/base/with-default-fallthrough/*",
"/base/nested/no-fallthrough/*",
]);
});
});
Expand Down

0 comments on commit 7c780cc

Please sign in to comment.