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 cssesc from breaking browser code #10194

Merged
merged 8 commits into from
Feb 22, 2024
Merged
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 .changeset/cuddly-ads-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/vercel": patch
---

Fix loading client-scripts in dev with ISR
5 changes: 5 additions & 0 deletions .changeset/quick-bottles-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes an issue related to content collections usage in browser context caused by `csssec`
13 changes: 9 additions & 4 deletions packages/astro/src/content/vite-plugin-content-virtual-mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ export function astroContentVirtualModPlugin({
}
}
},
async load(id) {
async load(id, args) {
if (id === RESOLVED_VIRTUAL_MODULE_ID) {
const lookupMap = await generateLookupMap({
settings,
fs,
});
const code = await generateContentEntryFile({ settings, fs, lookupMap, IS_DEV, IS_SERVER });
const isClient = !args?.ssr;
const code = await generateContentEntryFile({ settings, fs, lookupMap, IS_DEV, IS_SERVER, isClient });

return {
code,
Expand Down Expand Up @@ -102,12 +103,14 @@ export async function generateContentEntryFile({
lookupMap,
IS_DEV,
IS_SERVER,
isClient
}: {
settings: AstroSettings;
fs: typeof nodeFs;
lookupMap: ContentLookupMap;
IS_DEV: boolean;
IS_SERVER: boolean;
isClient: boolean;
}) {
const contentPaths = getContentPaths(settings.config);
const relContentDir = rootRelativePath(settings.config.root, contentPaths.contentDir);
Expand Down Expand Up @@ -143,13 +146,15 @@ export async function generateContentEntryFile({
renderEntryGlobResult = getStringifiedCollectionFromLookup('render', relContentDir, lookupMap);
}

const virtualModContents = nodeFs
let virtualModContents = nodeFs
.readFileSync(contentPaths.virtualModTemplate, 'utf-8')
.replace('@@CONTENT_DIR@@', relContentDir)
.replace("'@@CONTENT_ENTRY_GLOB_PATH@@'", contentEntryGlobResult)
.replace("'@@DATA_ENTRY_GLOB_PATH@@'", dataEntryGlobResult)
.replace("'@@RENDER_ENTRY_GLOB_PATH@@'", renderEntryGlobResult)
.replace('/* @@LOOKUP_MAP_ASSIGNMENT@@ */', `lookupMap = ${JSON.stringify(lookupMap)};`);
.replace('/* @@LOOKUP_MAP_ASSIGNMENT@@ */', `lookupMap = ${JSON.stringify(lookupMap)};`) +
(isClient ? `
console.warn('astro:content is only supported running server-side. Using it in the browser will lead to bloated bundles and slow down page load. In the future it will not be supported.');` : '');

return virtualModContents;
}
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/build/plugins/plugin-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ function vitePluginContent(
lookupMap,
IS_DEV: false,
IS_SERVER: false,
isClient: false,
});
this.emitFile({
type: 'prebuilt-chunk',
Expand Down
7 changes: 7 additions & 0 deletions packages/astro/src/transitions/vite-plugin-transitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ const resolvedVirtualClientModuleId = '\0' + virtualClientModuleId;
export default function astroTransitions({ settings }: { settings: AstroSettings }): vite.Plugin {
return {
name: 'astro:transitions',
config() {
return {
optimizeDeps: {
include: ['astro > cssesc'],
},
};
},
async resolveId(id) {
if (id === virtualModuleId) {
return resolvedVirtualModuleId;
Expand Down
14 changes: 0 additions & 14 deletions packages/integrations/vercel/src/serverless/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,20 +287,6 @@ export default function vercelServerless({
);
}
},
'astro:server:setup'({ server }) {
// isr functions do not have access to search params, this middleware removes them for the dev mode
if (isr) {
const exclude_ = typeof isr === 'object' ? isr.exclude ?? [] : [];
// we create a regex to emulate vercel's production behavior
const exclude = exclude_.concat('/_image').map((ex) => new RegExp(escapeRegex(ex)));
server.middlewares.use(function removeIsrParams(req, _, next) {
const { pathname } = new URL(`https://example.com${req.url}`);
if (exclude.some((ex) => ex.test(pathname))) return next();
req.url = pathname;
return next();
});
}
},
'astro:build:ssr': async ({ entryPoints, middlewareEntryPoint }) => {
_entryPoints = entryPoints;
_middlewareEntryPoint = middlewareEntryPoint;
Expand Down
Loading