From 367e61776196a17d61c28daa4dfbabb6244e040c Mon Sep 17 00:00:00 2001 From: Arsh <69170106+lilnasy@users.noreply.github.com> Date: Tue, 2 May 2023 01:45:04 +0530 Subject: [PATCH] Make prerendering decision based on `PageBuildData` instead of `BuildInternals` (#6956) * read prerender flag from PageBuildData * add changeset --- .changeset/popular-bats-pump.md | 5 +++++ packages/astro/src/core/build/generate.ts | 12 ++++------- packages/astro/src/core/build/internal.ts | 20 ++----------------- .../src/core/build/plugins/plugin-ssr.ts | 8 +++++--- packages/astro/src/core/build/static-build.ts | 7 ++++--- 5 files changed, 20 insertions(+), 32 deletions(-) create mode 100644 .changeset/popular-bats-pump.md diff --git a/.changeset/popular-bats-pump.md b/.changeset/popular-bats-pump.md new file mode 100644 index 000000000..ead449e62 --- /dev/null +++ b/.changeset/popular-bats-pump.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Changed where various parts of the build pipeline look to decide if a page should be prerendered. They now exclusively consider PageBuildData, allowing integrations to participate in the decision. diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index 812ae673f..6e50d687f 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -39,12 +39,7 @@ import { createRequest } from '../request.js'; import { matchRoute } from '../routing/match.js'; import { getOutputFilename } from '../util.js'; import { getOutDirWithinCwd, getOutFile, getOutFolder } from './common.js'; -import { - eachPageData, - eachPrerenderedPageData, - getPageDataByComponent, - sortedCSS, -} from './internal.js'; +import { eachPageData, getPageDataByComponent, sortedCSS } from './internal.js'; import type { PageBuildData, SingleFileBuiltModule, StaticBuildOptions } from './types'; import { getTimeStat } from './util.js'; @@ -99,8 +94,9 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn const builtPaths = new Set(); if (ssr) { - for (const pageData of eachPrerenderedPageData(internals)) { - await generatePage(opts, internals, pageData, ssrEntry, builtPaths); + for (const pageData of eachPageData(internals)) { + if (pageData.route.prerender) + await generatePage(opts, internals, pageData, ssrEntry, builtPaths); } } else { for (const pageData of eachPageData(internals)) { diff --git a/packages/astro/src/core/build/internal.ts b/packages/astro/src/core/build/internal.ts index e54405724..8cd052ffa 100644 --- a/packages/astro/src/core/build/internal.ts +++ b/packages/astro/src/core/build/internal.ts @@ -216,30 +216,14 @@ export function* eachPageData(internals: BuildInternals) { } export function hasPrerenderedPages(internals: BuildInternals) { - for (const id of internals.pagesByViteID.keys()) { - if (internals.pageOptionsByPage.get(id)?.prerender) { + for (const pageData of eachPageData(internals)) { + if (pageData.route.prerender) { return true; } } return false; } -export function* eachPrerenderedPageData(internals: BuildInternals) { - for (const [id, pageData] of internals.pagesByViteID.entries()) { - if (internals.pageOptionsByPage.get(id)?.prerender) { - yield pageData; - } - } -} - -export function* eachServerPageData(internals: BuildInternals) { - for (const [id, pageData] of internals.pagesByViteID.entries()) { - if (!internals.pageOptionsByPage.get(id)?.prerender) { - yield pageData; - } - } -} - /** * Sort a page's CSS by depth. A higher depth means that the CSS comes from shared subcomponents. * A lower depth means it comes directly from the top-level page. diff --git a/packages/astro/src/core/build/plugins/plugin-ssr.ts b/packages/astro/src/core/build/plugins/plugin-ssr.ts index 1133fc8e0..e5bca2ad0 100644 --- a/packages/astro/src/core/build/plugins/plugin-ssr.ts +++ b/packages/astro/src/core/build/plugins/plugin-ssr.ts @@ -13,7 +13,7 @@ import { joinPaths, prependForwardSlash } from '../../path.js'; import { serializeRouteData } from '../../routing/index.js'; import { addRollupInput } from '../add-rollup-input.js'; import { getOutFile, getOutFolder } from '../common.js'; -import { eachPrerenderedPageData, eachServerPageData, sortedCSS } from '../internal.js'; +import { eachPageData, sortedCSS } from '../internal.js'; import type { AstroBuildPlugin } from '../plugin'; export const virtualModuleId = '@astrojs-ssr-virtual-entry'; @@ -142,7 +142,8 @@ function buildManifest( } }; - for (const pageData of eachPrerenderedPageData(internals)) { + for (const pageData of eachPageData(internals)) { + if (!pageData.route.prerender) continue; if (!pageData.route.pathname) continue; const outFolder = getOutFolder( @@ -166,7 +167,8 @@ function buildManifest( staticFiles.push(file); } - for (const pageData of eachServerPageData(internals)) { + for (const pageData of eachPageData(internals)) { + if (pageData.route.prerender) continue; const scripts: SerializedRouteInfo['scripts'] = []; if (pageData.hoistedScript) { const hoistedValue = pageData.hoistedScript.value; diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 1ef57d787..ff71e80b8 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -8,7 +8,7 @@ import { fileURLToPath } from 'url'; import * as vite from 'vite'; import { createBuildInternals, - eachPrerenderedPageData, + eachPageData, type BuildInternals, } from '../../core/build/internal.js'; import { emptyDir, removeEmptyDirs } from '../../core/fs/index.js'; @@ -290,8 +290,9 @@ async function runPostBuildHooks( */ async function cleanStaticOutput(opts: StaticBuildOptions, internals: BuildInternals) { const allStaticFiles = new Set(); - for (const pageData of eachPrerenderedPageData(internals)) { - allStaticFiles.add(internals.pageToBundleMap.get(pageData.moduleSpecifier)); + for (const pageData of eachPageData(internals)) { + if (pageData.route.prerender) + allStaticFiles.add(internals.pageToBundleMap.get(pageData.moduleSpecifier)); } const ssr = opts.settings.config.output === 'server'; const out = ssr ? opts.buildConfig.server : getOutDirWithinCwd(opts.settings.config.outDir);