From 9745009ae0e7fe8691c75657c5c9586a548bf2e0 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Wed, 19 Oct 2022 07:00:52 -0700 Subject: [PATCH] Fixes index page with build.format=file (#5123) * Fixes index page with build.format=file * Adding a changeset --- .changeset/many-lions-relate.md | 5 +++ packages/astro/src/core/build/common.ts | 3 +- packages/astro/src/core/build/generate.ts | 29 -------------- .../test/dynamic-route-build-file.test.js | 24 ++++++++++++ .../dynamic-route-build-file/index.html | 10 +++++ .../dynamic-route-build-file/package.json | 8 ++++ .../src/pages/[...slug].astro | 38 +++++++++++++++++++ pnpm-lock.yaml | 6 +++ 8 files changed, 93 insertions(+), 30 deletions(-) create mode 100644 .changeset/many-lions-relate.md create mode 100644 packages/astro/test/dynamic-route-build-file.test.js create mode 100644 packages/astro/test/fixtures/dynamic-route-build-file/index.html create mode 100644 packages/astro/test/fixtures/dynamic-route-build-file/package.json create mode 100644 packages/astro/test/fixtures/dynamic-route-build-file/src/pages/[...slug].astro diff --git a/.changeset/many-lions-relate.md b/.changeset/many-lions-relate.md new file mode 100644 index 000000000..3c9c04bb5 --- /dev/null +++ b/.changeset/many-lions-relate.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes index page with build.format=file diff --git a/packages/astro/src/core/build/common.ts b/packages/astro/src/core/build/common.ts index 94c96a263..e05ce6d9e 100644 --- a/packages/astro/src/core/build/common.ts +++ b/packages/astro/src/core/build/common.ts @@ -30,7 +30,8 @@ export function getOutFolder( return new URL('.' + appendForwardSlash(pathname), outRoot); } case 'file': { - return new URL('.' + appendForwardSlash(npath.dirname(pathname)), outRoot); + const d = pathname === '' ? pathname : npath.dirname(pathname); + return new URL('.' + appendForwardSlash(d), outRoot); } } } diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index 8d2622cfe..e89fc0b28 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -34,35 +34,6 @@ import { eachPageData, getPageDataByComponent, sortedCSS } from './internal.js'; import type { PageBuildData, SingleFileBuiltModule, StaticBuildOptions } from './types'; import { getTimeStat } from './util.js'; -// Render is usually compute, which Node.js can't parallelize well. -// In real world testing, dropping from 10->1 showed a notiable perf -// improvement. In the future, we can revisit a smarter parallel -// system, possibly one that parallelizes if async IO is detected. -const MAX_CONCURRENT_RENDERS = 1; - -// Throttle the rendering a paths to prevents creating too many Promises on the microtask queue. -function* throttle(max: number, inPaths: string[]) { - let tmp = []; - let i = 0; - for (let path of inPaths) { - tmp.push(path); - if (i === max) { - yield tmp; - // Empties the array, to avoid allocating a new one. - tmp.length = 0; - i = 0; - } else { - i++; - } - } - - // If tmp has items in it, that means there were less than {max} paths remaining - // at the end, so we need to yield these too. - if (tmp.length) { - yield tmp; - } -} - function shouldSkipDraft(pageModule: ComponentInstance, settings: AstroSettings): boolean { return ( // Drafts are disabled diff --git a/packages/astro/test/dynamic-route-build-file.test.js b/packages/astro/test/dynamic-route-build-file.test.js new file mode 100644 index 000000000..cfa3eb0a5 --- /dev/null +++ b/packages/astro/test/dynamic-route-build-file.test.js @@ -0,0 +1,24 @@ +import { expect } from 'chai'; +import * as cheerio from 'cheerio'; +import { loadFixture } from './test-utils.js'; + +describe('build.format=file with dynamic routes', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/dynamic-route-build-file', + build: { + format: 'file' + } + }); + await fixture.build(); + }); + + it('Outputs a slug of undefined as the index.html', async () => { + const html = await fixture.readFile('/index.html'); + const $ = cheerio.load(html); + expect($('h1').text()).to.equal('Astro Store'); + }); +}); diff --git a/packages/astro/test/fixtures/dynamic-route-build-file/index.html b/packages/astro/test/fixtures/dynamic-route-build-file/index.html new file mode 100644 index 000000000..648198a4b --- /dev/null +++ b/packages/astro/test/fixtures/dynamic-route-build-file/index.html @@ -0,0 +1,10 @@ + + + + Astro Store + + +

Astro Store

+

Welcome to the Astro store!

+ + \ No newline at end of file diff --git a/packages/astro/test/fixtures/dynamic-route-build-file/package.json b/packages/astro/test/fixtures/dynamic-route-build-file/package.json new file mode 100644 index 000000000..fa5e82491 --- /dev/null +++ b/packages/astro/test/fixtures/dynamic-route-build-file/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/dynamic-route-build-file", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/dynamic-route-build-file/src/pages/[...slug].astro b/packages/astro/test/fixtures/dynamic-route-build-file/src/pages/[...slug].astro new file mode 100644 index 000000000..8b04dfcff --- /dev/null +++ b/packages/astro/test/fixtures/dynamic-route-build-file/src/pages/[...slug].astro @@ -0,0 +1,38 @@ +--- +export async function getStaticPaths() { + const pages = [ + { + slug: undefined, + title: "Astro Store", + text: "Welcome to the Astro store!", + }, + { + slug: "products", + title: "Astro products", + text: "We have lots of products for you", + }, + { + slug: "products/astro-handbook", + title: "The ultimative Astro handbook", + text: "If you want to learn Astro, you must read this book.", + }, + ]; + return pages.map(({ slug, title, text }) => { + return { + params: { slug }, + props: { title, text }, + }; + }); +} + +const { title, text } = Astro.props; +--- + + + {title} + + +

{title}

+

{text}

+ + diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cc8ab1627..3d2a9783c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1676,6 +1676,12 @@ importers: dependencies: astro: link:../../.. + packages/astro/test/fixtures/dynamic-route-build-file: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + packages/astro/test/fixtures/entry-file-names: specifiers: '@astrojs/preact': 'workspace:'