mirror of
https://github.com/withastro/astro.git
synced 2025-01-27 22:19:04 -05:00
Prevent usage of astro:content in the client (#11827)
* Prevent usage of astro:content in the client * Fix build errors * Update .changeset/neat-dots-hear.md Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> * Throw an AstroError * Just throw --------- Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
This commit is contained in:
parent
7315050fc1
commit
a83e362ee4
2 changed files with 28 additions and 13 deletions
11
.changeset/neat-dots-hear.md
Normal file
11
.changeset/neat-dots-hear.md
Normal file
|
@ -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.
|
|
@ -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;
|
||||
}
|
||||
|
@ -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,
|
||||
|
|
Loading…
Add table
Reference in a new issue