Skip to content

Commit

Permalink
Make prerendering decision based on PageBuildData instead of `Build…
Browse files Browse the repository at this point in the history
…Internals` (#6956)

* read prerender flag from PageBuildData

* add changeset
  • Loading branch information
lilnasy authored May 1, 2023
1 parent 895fa07 commit 367e617
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-bats-pump.md
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 4 additions & 8 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -99,8 +94,9 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn
const builtPaths = new Set<string>();

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)) {
Expand Down
20 changes: 2 additions & 18 deletions packages/astro/src/core/build/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions packages/astro/src/core/build/plugins/plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(
Expand All @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 367e617

Please sign in to comment.