Skip to content

Commit

Permalink
Fix cssesc from breaking browser code (withastro#10194)
Browse files Browse the repository at this point in the history
* Fix cssesc from breaking browser code

* Include specific thing instead

* Update .changeset/quick-bottles-march.md

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>

* Fix ISR

* Remove query stripping altogether

* Warn on client usage

* Fix build

* oops

---------

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
  • Loading branch information
2 people authored and log101 committed Feb 23, 2024
1 parent 65de227 commit 41bd279
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
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

0 comments on commit 41bd279

Please sign in to comment.