Skip to content

Commit

Permalink
Prevent usage of astro:content in the client (#11827)
Browse files Browse the repository at this point in the history
* Prevent usage of astro:content in the client

* Fix build errors

* Update .changeset/neat-dots-hear.md

Co-authored-by: Emanuele Stoppa <[email protected]>

* Throw an AstroError

* Just throw

---------

Co-authored-by: Emanuele Stoppa <[email protected]>
  • Loading branch information
matthewp and ematipico authored Aug 27, 2024
1 parent 7315050 commit a83e362
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
11 changes: 11 additions & 0 deletions .changeset/neat-dots-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'astro': major
---

Prevent usage of `astro:content` in the client

Usage of `astro:content` in the client has always been discouraged because it leads to all of your content winding up in your client bundle, and can possibly leaks secrets.

This formally makes doing so impossible, adding to the previous warning with errors.

In the future Astro might add APIs for client-usage based on needs.
30 changes: 17 additions & 13 deletions packages/astro/src/content/vite-plugin-content-virtual-mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,22 @@ export async function generateContentEntryFile({
renderEntryGlobResult = getStringifiedCollectionFromLookup('render', relContentDir, lookupMap);
}

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)};`) +
(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.');`
: '');
let virtualModContents: string;
if(isClient) {
throw new AstroError({
...AstroErrorData.ServerOnlyModule,
message: AstroErrorData.ServerOnlyModule.message('astro:content'),
});
} else {
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)};`);
}

return virtualModContents;
}
Expand Down Expand Up @@ -360,7 +364,7 @@ export async function generateLookupMap({
const contentEntryType = contentEntryConfigByExt.get(extname(filePath));
if (!contentEntryType) throw UnexpectedLookupMapError;

const { id, slug: generatedSlug } = await getContentEntryIdAndSlug({
const { id, slug: generatedSlug } = getContentEntryIdAndSlug({
entry: pathToFileURL(filePath),
contentDir,
collection,
Expand Down

0 comments on commit a83e362

Please sign in to comment.