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: prevent conflict between Netlify Identity and edge function rendering #12052

53 changes: 32 additions & 21 deletions packages/adapter-netlify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,42 @@ async function generate_edge_functions({ builder }) {

builder.mkdirp('.netlify/edge-functions');

builder.log.minor('Generating Edge Function...');
const relativePath = posix.relative(tmp, builder.getServerDirectory());

builder.copy(`${files}/edge.js`, `${tmp}/entry.js`, {
replace: {
'0SERVER': `${relativePath}/index.js`,
MANIFEST: './manifest.js'
}
});

const manifest = builder.generateManifest({
relativePath
});

writeFileSync(`${tmp}/manifest.js`, `export const manifest = ${manifest};\n`);

/** @type {{ assets: Set<string> }} */
const { assets } = (await import(`${tmp}/manifest.js`)).manifest;

const path = '/*';
Copy link
Member

Choose a reason for hiding this comment

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

Wonder if we should add the base path to this:

const path = `${builder.config.kit.paths.base}/*`;

// We only need to specify paths without the trailing slash because
// Netlify will handle the optional trailing slash for us
const excludedPath = [
// Contains static files
`/${builder.getAppPath()}/*`,
...builder.prerendered.paths,
...Array.from(assets).flatMap((asset) => {
Copy link
Member

Choose a reason for hiding this comment

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

the previous logic also checked within manifest._.server_assets, this one does not - oversight or on purpose?

Copy link
Member

@eltigerchino eltigerchino Jan 16, 2025

Choose a reason for hiding this comment

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

We could add manifest._.server_assets too but they're currently already included by the immutable assets exclusion /${builder.getAppPath()}/* because we copy the server assets over and make them public along with the client assets during the build process. I'm not sure if that will change in the future.

copy(
`${out}/server/${kit.appDir}/immutable/assets`,
`${out}/client/${kit.appDir}/immutable/assets`
);

if (asset.endsWith('/index.html')) {
const dir = asset.replace(/\/index\.html$/, '');
return [
`${builder.config.kit.paths.base}/${asset}`,
`${builder.config.kit.paths.base}/${dir}`
];
}
return `${builder.config.kit.paths.base}/${asset}`;
}),
// Should not be served by SvelteKit at all
'/.netlify/*'
];
Expand All @@ -146,27 +178,6 @@ async function generate_edge_functions({ builder }) {
version: 1
};

builder.log.minor('Generating Edge Function...');
const relativePath = posix.relative(tmp, builder.getServerDirectory());

builder.copy(`${files}/edge.js`, `${tmp}/entry.js`, {
replace: {
'0SERVER': `${relativePath}/index.js`,
MANIFEST: './manifest.js'
}
});

const manifest = builder.generateManifest({
relativePath
});

writeFileSync(
`${tmp}/manifest.js`,
`export const manifest = ${manifest};\n\nexport const prerendered = new Set(${JSON.stringify(
builder.prerendered.paths
)});\n`
);

await esbuild.build({
entryPoints: [`${tmp}/entry.js`],
outfile: '.netlify/edge-functions/render.js',
Expand Down
41 changes: 1 addition & 40 deletions packages/adapter-netlify/src/edge.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Server } from '0SERVER';
import { manifest, prerendered } from 'MANIFEST';
import { manifest } from 'MANIFEST';

const server = new Server(manifest);
const prefix = `/${manifest.appPath}/`;

const initialized = server.init({
// @ts-ignore
Expand All @@ -15,12 +14,6 @@ const initialized = server.init({
* @returns { Promise<Response> }
*/
export default async function handler(request, context) {
if (is_static_file(request)) {
// Static files can skip the handler
// TODO can we serve _app/immutable files with an immutable cache header?
return;
}

await initialized;
return server.respond(request, {
platform: { context },
Expand All @@ -29,35 +22,3 @@ export default async function handler(request, context) {
}
});
}

/**
* @param {Request} request
*/
function is_static_file(request) {
const url = new URL(request.url);

// Assets in the app dir
// TODO(serhalp) We no longer run the edge function on these at all. Can we remove this check?
if (url.pathname.startsWith(prefix)) {
return true;
}

// prerendered pages and index.html files
const pathname = url.pathname.replace(/\/$/, '');
let file = pathname.substring(1);

try {
file = decodeURIComponent(file);
} catch {
// ignore
}

// TODO(serhalp) Now that edge function config supports `excludedPath`, could we replace all these checks?
return (
manifest.assets.has(file) ||
manifest.assets.has(file + '/index.html') ||
file in manifest._.server_assets ||
file + '/index.html' in manifest._.server_assets ||
prerendered.has(pathname || '/')
);
}
Loading