Merge pull request #995 from mulatta/fix/plugin-sandbox-cors

fix: allow plugin sandbox chunks to load with CORS
This commit is contained in:
Linus Rath
2026-09-17 22:03:47 +02:00
committed by GitHub
2 changed files with 50 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import type { NextConfig } from 'next';
async function loadConfig(basePath?: string): Promise<NextConfig> {
vi.resetModules();
vi.stubEnv('NEXT_PUBLIC_BASE_PATH', basePath);
const { default: config } = await import('../../next.config');
return config;
}
afterEach(() => {
vi.unstubAllEnvs();
vi.resetModules();
});
describe('Next static asset CORS', () => {
it.each([undefined, '/webmail'])('allows opaque plugin sandboxes with basePath %s', async (basePath) => {
const config = await loadConfig(basePath);
const rules = await config.headers?.();
expect(config.basePath).toBe(basePath);
expect(rules?.filter((rule) => rule.source.includes('_next/static'))).toEqual([
{
source: '/_next/static/:path*',
headers: [{ key: 'Access-Control-Allow-Origin', value: '*' }],
},
]);
});
it('does not add CORS headers to application or API routes', async () => {
const config = await loadConfig('/webmail');
const rules = await config.headers?.();
const corsRules = rules?.filter((rule) =>
rule.headers.some((header) => header.key.toLowerCase() === 'access-control-allow-origin')
);
expect(corsRules?.map((rule) => rule.source)).toEqual(['/_next/static/:path*']);
});
});
+10
View File
@@ -46,6 +46,16 @@ const nextConfig: NextConfig = {
// it from node_modules at runtime instead of trying to bundle it. Used by
// PLUGIN_DEV_DIR's on-the-fly bundler.
serverExternalPackages: ["esbuild"],
async headers() {
return [
{
// Untrusted plugin iframes have opaque origins and load these public,
// immutable chunks in CORS mode. Next prefixes this source with basePath.
source: "/_next/static/:path*",
headers: [{ key: "Access-Control-Allow-Origin", value: "*" }],
},
];
},
// Sibling repos checked out under ./repos/ are unrelated source trees that
// Turbopack's NFT can otherwise rope into the trace when dynamic fs calls
// confuse it. Keeps the build from ballooning memory tracing dead code.