From aff7ddccddd9e66b3970fb59fe72ad7b5de033f3 Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Sat, 20 May 2023 12:53:59 +0000 Subject: [PATCH 01/18] `isPrenderDefault` ---> `isPrerenderDefault` --- packages/astro/src/core/routing/manifest/create.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/astro/src/core/routing/manifest/create.ts b/packages/astro/src/core/routing/manifest/create.ts index 51f495e8b468..a673e199ba2f 100644 --- a/packages/astro/src/core/routing/manifest/create.ts +++ b/packages/astro/src/core/routing/manifest/create.ts @@ -227,7 +227,7 @@ export function createRouteManifest( ]); const validEndpointExtensions: Set = new Set(['.js', '.ts']); const localFs = fsMod ?? nodeFs; - const isPrenderDefault = isHybridOutput(settings.config); + const isPrerenderDefault = isHybridOutput(settings.config); const foundInvalidFileExtensions: Set = new Set(); @@ -340,7 +340,7 @@ export function createRouteManifest( component, generate, pathname: pathname || undefined, - prerender: isPrenderDefault, + prerender: isPrerenderDefault, }); } }); @@ -416,7 +416,7 @@ export function createRouteManifest( component, generate, pathname: pathname || void 0, - prerender: isPrenderDefault, + prerender: isPrerenderDefault, }); }); From 7fddcc7ff2c280f4855743b659c9e0df6914440a Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Sat, 20 May 2023 15:52:04 +0000 Subject: [PATCH 02/18] test: add test fixture --- .../ssr-hybrid-get-static-paths/package.json | 9 ++++++ .../src/pages/[...calledTwiceTest].astro | 21 ++++++++++++ .../src/pages/blog/[year]/[slug].astro | 18 +++++++++++ .../src/pages/data/[slug].json.ts | 14 ++++++++ .../src/pages/food/[name].astro | 32 +++++++++++++++++++ .../src/pages/nested-arrays/[slug].astro | 9 ++++++ .../src/pages/pizza/[...pizza].astro | 22 +++++++++++++ .../src/pages/pizza/[cheese]-[topping].astro | 21 ++++++++++++ .../src/pages/posts/[page].astro | 29 +++++++++++++++++ 9 files changed, 175 insertions(+) create mode 100644 packages/astro/test/fixtures/ssr-hybrid-get-static-paths/package.json create mode 100644 packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/[...calledTwiceTest].astro create mode 100644 packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/blog/[year]/[slug].astro create mode 100644 packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/data/[slug].json.ts create mode 100644 packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/food/[name].astro create mode 100644 packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/nested-arrays/[slug].astro create mode 100644 packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/pizza/[...pizza].astro create mode 100644 packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/pizza/[cheese]-[topping].astro create mode 100644 packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/posts/[page].astro diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/package.json b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/package.json new file mode 100644 index 000000000000..6847c5caac6c --- /dev/null +++ b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/ssr-hybrid-get-static-paths", + "version": "0.0.0", + "private": true, + "dependencies": { + "@astrojs/node": "*", + "astro": "*" + } +} diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/[...calledTwiceTest].astro b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/[...calledTwiceTest].astro new file mode 100644 index 000000000000..08b6af30c085 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/[...calledTwiceTest].astro @@ -0,0 +1,21 @@ +--- +export function getStaticPaths({ paginate }) { + if (globalThis.isCalledOnce) { + throw new Error("Can only be called once!"); + } + globalThis.isCalledOnce = true; + return [ + {params: {calledTwiceTest: 'a'}}, + {params: {calledTwiceTest: 'b'}}, + {params: {calledTwiceTest: 'c'}}, + ]; +} +const { params } = Astro; +--- + + + + Page {params.calledTwiceTest} + + + diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/blog/[year]/[slug].astro b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/blog/[year]/[slug].astro new file mode 100644 index 000000000000..95902cf07b7e --- /dev/null +++ b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/blog/[year]/[slug].astro @@ -0,0 +1,18 @@ +--- +export async function getStaticPaths() { + return [ + { params: { year: '2022', slug: 'post-1' } }, + { params: { year: 2022, slug: 'post-2' } }, + { params: { slug: 'post-2', year: '2022' } }, + ] +} + +const { year, slug } = Astro.params +--- + + + + {year} | {slug} + + + diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/data/[slug].json.ts b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/data/[slug].json.ts new file mode 100644 index 000000000000..3c7cc63baa00 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/data/[slug].json.ts @@ -0,0 +1,14 @@ +export async function getStaticPaths() { + return [ + { params: { slug: 'thing1' } }, + { params: { slug: 'thing2' } } + ]; +} + +export async function get() { + return { + body: JSON.stringify({ + title: '[slug]' + }, null, 4) + }; +} diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/food/[name].astro b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/food/[name].astro new file mode 100644 index 000000000000..e50ab3654e05 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/food/[name].astro @@ -0,0 +1,32 @@ +--- +export async function getStaticPaths() { + return [ + { + params: { name: 'tacos' }, + props: { yum: 10 }, + }, + { + params: { name: 'potatoes' }, + props: { yum: 7 }, + }, + { + params: { name: 'spaghetti' }, + props: { yum: 5 }, + } + ] +} + +const { yum } = Astro.props; +--- + + + + + + Food + + +

{ Astro.url.pathname }

+

{ yum }

+ + diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/nested-arrays/[slug].astro b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/nested-arrays/[slug].astro new file mode 100644 index 000000000000..0282fb139a2a --- /dev/null +++ b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/nested-arrays/[slug].astro @@ -0,0 +1,9 @@ +--- + export function getStaticPaths() { + return [ + [ { params: {slug: "slug1"} } ], + [ { params: {slug: "slug2"} } ], + ] + } + +--- diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/pizza/[...pizza].astro b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/pizza/[...pizza].astro new file mode 100644 index 000000000000..a58b314e36e4 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/pizza/[...pizza].astro @@ -0,0 +1,22 @@ +--- +export function getStaticPaths() { + return [{ + params: { pizza: 'papa-johns' }, + }, { + params: { pizza: 'dominos' }, + }, { + params: { pizza: 'grimaldis/new-york' }, + }] +} +const { pizza } = Astro.params +--- + + + + + {pizza ?? 'The landing page'} + + +

Welcome to {pizza ?? 'The landing page'}

+ + diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/pizza/[cheese]-[topping].astro b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/pizza/[cheese]-[topping].astro new file mode 100644 index 000000000000..a698a76d784a --- /dev/null +++ b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/pizza/[cheese]-[topping].astro @@ -0,0 +1,21 @@ +--- +export function getStaticPaths() { + return [{ + params: { cheese: 'mozzarella', topping: 'pepperoni' }, + }, { + params: { cheese: 'provolone', topping: 'sausage' }, + }] +} +const { cheese, topping } = Astro.params +--- + + + + + {cheese} + + +

🍕 It's pizza time

+

{cheese}-{topping}

+ + diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/posts/[page].astro b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/posts/[page].astro new file mode 100644 index 000000000000..c00edc2f5196 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/posts/[page].astro @@ -0,0 +1,29 @@ +--- +export async function getStaticPaths() { + return [ + { + params: { page: 1 }, + }, + { + params: { page: 2 }, + }, + { + params: { page: 3 } + } + ] +}; +const { page } = Astro.params +const canonicalURL = new URL(Astro.url.pathname, Astro.site); +--- + + + + + + Posts Page {page} + + + +

Welcome to page {page}

+ + From e3aa4c1c300e51c228427056b42a6d8523e85158 Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Sat, 20 May 2023 15:53:26 +0000 Subject: [PATCH 03/18] test: add hybrid getStaticPaths tests --- .../ssr-prerender-get-static-paths.test.js | 349 +++++++++++++----- 1 file changed, 259 insertions(+), 90 deletions(-) diff --git a/packages/astro/test/ssr-prerender-get-static-paths.test.js b/packages/astro/test/ssr-prerender-get-static-paths.test.js index f7100372b6fa..8f9ecdacaadb 100644 --- a/packages/astro/test/ssr-prerender-get-static-paths.test.js +++ b/packages/astro/test/ssr-prerender-get-static-paths.test.js @@ -1,131 +1,300 @@ import { expect } from 'chai'; import { loadFixture } from './test-utils.js'; import * as cheerio from 'cheerio'; +import testAdapter from './test-adapter.js'; -describe('prerender getStaticPaths - build calls', () => { - /** @type {import('./test-utils').Fixture} */ - let fixture; +describe('Prerender', () => { + const sharedConfig = { + site: 'https://mysite.dev/', + adapter: testAdapter(), + base: '/blog', + output: 'server', + }; + describe('getStaticPaths - build calls', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + /** @type {Error} */ + let error; + before(async () => { + fixture = await loadFixture({ + root: './fixtures/ssr-prerender-get-static-paths/', + ...sharedConfig, + }); + try { + await fixture.build(); + } catch (err) { + error = err; + } + }); - before(async () => { - fixture = await loadFixture({ - root: './fixtures/ssr-prerender-get-static-paths/', - site: 'https://mysite.dev/', - base: '/blog', + afterEach(() => { + // reset the flag used by [...calledTwiceTest].astro between each test + globalThis.isCalledOnce = false; }); - await fixture.build(); - }); - afterEach(() => { - // reset the flag used by [...calledTwiceTest].astro between each test - globalThis.isCalledOnce = false; - }); + it('is only called once during build', () => { + console.log({ error }); + if (error) throw error; + // useless expect; if build() throws in setup then this test fails + expect(true).to.equal(true); + }); - it('is only called once during build', () => { - // useless expect; if build() throws in setup then this test fails - expect(true).to.equal(true); + it('Astro.url sets the current pathname', async () => { + const html = await fixture.readFile('/client/food/tacos/index.html'); + const $ = cheerio.load(html); + + expect($('#props').text()).to.equal('10'); + expect($('#url').text()).to.equal('/blog/food/tacos/'); + }); }); - it('Astro.url sets the current pathname', async () => { - const html = await fixture.readFile('/food/tacos/index.html'); - const $ = cheerio.load(html); + describe('getStaticPaths - dev calls', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + let devServer; - expect($('#props').text()).to.equal('10'); - expect($('#url').text()).to.equal('/blog/food/tacos/'); - }); -}); + before(async () => { + globalThis.isCalledOnce = false; + fixture = await loadFixture({ + root: './fixtures/ssr-prerender-get-static-paths/', + ...sharedConfig, + }); + devServer = await fixture.startDevServer(); + }); -describe('prerender getStaticPaths - dev calls', () => { - let fixture; - let devServer; + afterEach(() => { + // reset the flag used by [...calledTwiceTest].astro between each test + globalThis.isCalledOnce = false; + }); - before(async () => { - globalThis.isCalledOnce = false; - fixture = await loadFixture({ - root: './fixtures/ssr-prerender-get-static-paths/', - site: 'https://mysite.dev/', + after(async () => { + devServer.stop(); }); - devServer = await fixture.startDevServer(); - }); - afterEach(() => { - // reset the flag used by [...calledTwiceTest].astro between each test - globalThis.isCalledOnce = false; - }); + it('only calls prerender getStaticPaths once', async () => { + let res = await fixture.fetch('/blog/a'); + expect(res.status).to.equal(200); - after(async () => { - devServer.stop(); - }); + res = await fixture.fetch('/blog/b'); + expect(res.status).to.equal(200); + + res = await fixture.fetch('/blog/c'); + expect(res.status).to.equal(200); + }); - it('only calls prerender getStaticPaths once', async () => { - let res = await fixture.fetch('/a'); - expect(res.status).to.equal(200); + describe('404 behavior', () => { + it('resolves 200 on matching static path - named params', async () => { + const res = await fixture.fetch('/blog/pizza/provolone-sausage'); + expect(res.status).to.equal(200); + }); - res = await fixture.fetch('/b'); - expect(res.status).to.equal(200); + it('resolves 404 on pattern match without static path - named params', async () => { + const res = await fixture.fetch('/blog/pizza/provolone-pineapple'); + const html = await res.text(); + console.log({ html }); + expect(res.status).to.equal(404); + expect(html).to.match(/404/); + }); + + it('resolves 200 on matching static path - rest params', async () => { + const res = await fixture.fetch('/blog/pizza/grimaldis/new-york'); + expect(res.status).to.equal(200); + }); + + it('resolves 404 on pattern match without static path - rest params', async () => { + const res = await fixture.fetch('/blog/pizza/pizza-hut'); + const html = await res.text(); + console.log({ html }); + + expect(res.status).to.equal(404); + expect(html).to.match(/404/); + }); + }); - res = await fixture.fetch('/c'); - expect(res.status).to.equal(200); + describe('route params type validation', () => { + it('resolves 200 on nested array parameters', async () => { + const res = await fixture.fetch('/blog/nested-arrays/slug1'); + expect(res.status).to.equal(200); + }); + + it('resolves 200 on matching static path - string params', async () => { + // route provided with { params: { year: "2022", slug: "post-2" }} + const res = await fixture.fetch('/blog/blog/2022/post-1'); + expect(res.status).to.equal(200); + }); + + it('resolves 200 on matching static path - numeric params', async () => { + // route provided with { params: { year: 2022, slug: "post-2" }} + const res = await fixture.fetch('/blog/blog/2022/post-2'); + expect(res.status).to.equal(200); + }); + }); + + it('resolves 200 on matching static paths', async () => { + // routes params provided for pages /posts/1, /posts/2, and /posts/3 + for (const page of [1, 2, 3]) { + let res = await fixture.fetch(`/blog/posts/${page}`); + expect(res.status).to.equal(200); + + const html = await res.text(); + const $ = cheerio.load(html); + + const canonical = $('link[rel=canonical]'); + expect(canonical.attr('href')).to.equal( + `https://mysite.dev/blog/posts/${page}`, + `doesn't trim the /${page} route param` + ); + } + }); }); +}); - describe('404 behavior', () => { - it('resolves 200 on matching static path - named params', async () => { - const res = await fixture.fetch('/pizza/provolone-sausage'); - expect(res.status).to.equal(200); +describe('Hybrid', () => { + const sharedConfig = { + site: 'https://mysite.dev/', + adapter: testAdapter(), + base: '/blog', + output: 'hybrid', + experimental: { + hybridOutput: true, + }, + }; + describe('getStaticPaths - build calls', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + /** @type {Error} */ + let error; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/ssr-hybrid-get-static-paths/', + ...sharedConfig, + }); + try { + await fixture.build(); + } catch (err) { + error = err; + } }); - it('resolves 404 on pattern match without static path - named params', async () => { - const res = await fixture.fetch('/pizza/provolone-pineapple'); - const html = await res.text(); - expect(res.status).to.equal(404); - expect(html).to.match(/404/); + afterEach(() => { + // reset the flag used by [...calledTwiceTest].astro between each test + globalThis.isCalledOnce = false; }); - it('resolves 200 on matching static path - rest params', async () => { - const res = await fixture.fetch('/pizza/grimaldis/new-york'); - expect(res.status).to.equal(200); + it('is only called once during build', () => { + console.log({ error }); + if (error) throw error; + // useless expect; if build() throws in setup then this test fails + expect(true).to.equal(true); }); - it('resolves 404 on pattern match without static path - rest params', async () => { - const res = await fixture.fetch('/pizza/pizza-hut'); - const html = await res.text(); - expect(res.status).to.equal(404); - expect(html).to.match(/404/); + it('Astro.url sets the current pathname', async () => { + const html = await fixture.readFile('/client/food/tacos/index.html'); + const $ = cheerio.load(html); + + expect($('#props').text()).to.equal('10'); + expect($('#url').text()).to.equal('/blog/food/tacos/'); }); }); - describe('route params type validation', () => { - it('resolves 200 on nested array parameters', async () => { - const res = await fixture.fetch('/nested-arrays/slug1'); - expect(res.status).to.equal(200); + describe('getStaticPaths - dev calls', () => { + let fixture; + let devServer; + + before(async () => { + globalThis.isCalledOnce = false; + fixture = await loadFixture({ + root: './fixtures/ssr-hybrid-get-static-paths/', + ...sharedConfig, + }); + devServer = await fixture.startDevServer(); }); - it('resolves 200 on matching static path - string params', async () => { - // route provided with { params: { year: "2022", slug: "post-2" }} - const res = await fixture.fetch('/blog/2022/post-1'); - expect(res.status).to.equal(200); + afterEach(() => { + // reset the flag used by [...calledTwiceTest].astro between each test + globalThis.isCalledOnce = false; }); - it('resolves 200 on matching static path - numeric params', async () => { - // route provided with { params: { year: 2022, slug: "post-2" }} - const res = await fixture.fetch('/blog/2022/post-2'); - expect(res.status).to.equal(200); + after(async () => { + devServer.stop(); }); - }); - it('resolves 200 on matching static paths', async () => { - // routes params provided for pages /posts/1, /posts/2, and /posts/3 - for (const page of [1, 2, 3]) { - let res = await fixture.fetch(`/posts/${page}`); + it('only calls hybrid getStaticPaths once', async () => { + let res = await fixture.fetch('/blog/a'); expect(res.status).to.equal(200); - const html = await res.text(); - const $ = cheerio.load(html); + res = await fixture.fetch('/blog/b'); + expect(res.status).to.equal(200); + + res = await fixture.fetch('/blog/c'); + expect(res.status).to.equal(200); + }); + + describe('404 behavior', () => { + it('resolves 200 on matching static path - named params', async () => { + const res = await fixture.fetch('/blog/pizza/provolone-sausage'); + expect(res.status).to.equal(200); + }); + + it('resolves 404 on pattern match without static path - named params', async () => { + const res = await fixture.fetch('/blog/pizza/provolone-pineapple'); + const html = await res.text(); + console.log({ html }); + expect(res.status).to.equal(404); + expect(html).to.match(/404/); + }); + + it('resolves 200 on matching static path - rest params', async () => { + const res = await fixture.fetch('/blog/pizza/grimaldis/new-york'); + expect(res.status).to.equal(200); + }); + + it('resolves 404 on pattern match without static path - rest params', async () => { + const res = await fixture.fetch('/blog/pizza/pizza-hut'); + const html = await res.text(); + console.log({ html }); + + expect(res.status).to.equal(404); + expect(html).to.match(/404/); + }); + }); - const canonical = $('link[rel=canonical]'); - expect(canonical.attr('href')).to.equal( - `https://mysite.dev/posts/${page}`, - `doesn't trim the /${page} route param` - ); - } + describe('route params type validation', () => { + it('resolves 200 on nested array parameters', async () => { + const res = await fixture.fetch('/blog/nested-arrays/slug1'); + expect(res.status).to.equal(200); + }); + + it('resolves 200 on matching static path - string params', async () => { + // route provided with { params: { year: "2022", slug: "post-2" }} + const res = await fixture.fetch('/blog/blog/2022/post-1'); + expect(res.status).to.equal(200); + }); + + it('resolves 200 on matching static path - numeric params', async () => { + // route provided with { params: { year: 2022, slug: "post-2" }} + const res = await fixture.fetch('/blog/blog/2022/post-2'); + expect(res.status).to.equal(200); + }); + }); + + it('resolves 200 on matching static paths', async () => { + // routes params provided for pages /posts/1, /posts/2, and /posts/3 + for (const page of [1, 2, 3]) { + let res = await fixture.fetch(`/blog/posts/${page}`); + expect(res.status).to.equal(200); + + const html = await res.text(); + const $ = cheerio.load(html); + + const canonical = $('link[rel=canonical]'); + expect(canonical.attr('href')).to.equal( + `https://mysite.dev/blog/posts/${page}`, + `doesn't trim the /${page} route param` + ); + } + }); }); }); From 9d2e1f2bc078bfa418ecf772dd3485ecb67ef443 Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Sat, 20 May 2023 15:54:36 +0000 Subject: [PATCH 04/18] try fix in ci --- packages/astro/src/core/build/generate.ts | 3 ++ .../core/build/plugins/plugin-prerender.ts | 9 ++++- packages/astro/src/core/endpoint/index.ts | 2 +- packages/astro/src/core/render/core.ts | 10 ++++- packages/astro/src/core/render/dev/index.ts | 6 +++ packages/astro/src/core/render/route-cache.ts | 10 +++-- packages/astro/src/core/routing/validation.ts | 11 ++++-- packages/astro/src/prerender/utils.ts | 24 ++++++++++++ .../src/vite-plugin-astro-server/route.ts | 13 ++++++- .../astro/src/vite-plugin-scanner/scan.ts | 1 + pnpm-lock.yaml | 39 +++++++++++++------ 11 files changed, 105 insertions(+), 23 deletions(-) diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index 452a902caf69..5f2180d58ac5 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -105,6 +105,7 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn if (ssr) { for (const pageData of eachPageData(internals)) { + console.log('Route prerendering: ', pageData.route.prerender); if (pageData.route.prerender) await generatePage(opts, internals, pageData, ssrEntry, builtPaths); } @@ -198,6 +199,7 @@ async function generatePage( // Get paths for the route, calling getStaticPaths if needed. const paths = await getPathsForRoute(pageData, pageModule, opts, builtPaths); + console.log({ paths }); for (let i = 0; i < paths.length; i++) { const path = paths[i]; @@ -244,6 +246,7 @@ async function getPathsForRoute( debug('build', `├── ${colors.bold(colors.red('✗'))} ${route.component}`); throw err; }); + console.log({ result }); // Save the route cache so it doesn't get called again opts.routeCache.set(route, result); diff --git a/packages/astro/src/core/build/plugins/plugin-prerender.ts b/packages/astro/src/core/build/plugins/plugin-prerender.ts index 7c9f3f7848ae..5cd2c387e8c9 100644 --- a/packages/astro/src/core/build/plugins/plugin-prerender.ts +++ b/packages/astro/src/core/build/plugins/plugin-prerender.ts @@ -3,8 +3,12 @@ import type { BuildInternals } from '../internal.js'; import type { AstroBuildPlugin } from '../plugin.js'; import type { StaticBuildOptions } from '../types'; import { extendManualChunks } from './util.js'; +import { getPrerenderMetadata } from '../../../prerender/utils.js'; -function vitePluginPrerender(opts: StaticBuildOptions, internals: BuildInternals): VitePlugin { +export function vitePluginPrerender( + opts: StaticBuildOptions, + internals: BuildInternals +): VitePlugin { return { name: 'astro:rollup-plugin-prerender', @@ -19,7 +23,8 @@ function vitePluginPrerender(opts: StaticBuildOptions, internals: BuildInternals if (pageInfo) { // prerendered pages should be split into their own chunk // Important: this can't be in the `pages/` directory! - if (meta.getModuleInfo(id)?.meta.astro?.pageOptions?.prerender) { + if (getPrerenderMetadata(meta.getModuleInfo(id))) { + console.log('Plugin prerender run!'); pageInfo.route.prerender = true; return 'prerender'; } diff --git a/packages/astro/src/core/endpoint/index.ts b/packages/astro/src/core/endpoint/index.ts index c005efb1f4e9..7d298a80226d 100644 --- a/packages/astro/src/core/endpoint/index.ts +++ b/packages/astro/src/core/endpoint/index.ts @@ -138,7 +138,7 @@ export async function callEndpoint }; } - if (env.ssr && !mod.prerender) { + if (env.ssr && !ctx.route?.prerender) { if (response.hasOwnProperty('headers')) { warn( logging, diff --git a/packages/astro/src/core/render/core.ts b/packages/astro/src/core/render/core.ts index 1c12a1a8d6e5..528d9d6964ff 100644 --- a/packages/astro/src/core/render/core.ts +++ b/packages/astro/src/core/render/core.ts @@ -49,6 +49,7 @@ export async function getParamsAndProps( // Handle dynamic routes let params: Params = {}; let pageProps: Props; + console.log('Looking for route!'); if (route && !route.pathname) { if (route.params.length) { // The RegExp pattern expects a decoded string, but the pathname is encoded @@ -89,7 +90,12 @@ export async function getParamsAndProps( routeCache.set(route, routeCacheEntry); } const matchedStaticPath = findPathItemByKey(routeCacheEntry.staticPaths, params, route); - if (!matchedStaticPath && (ssr ? mod.prerender : true)) { + console.log('getParamsAndProps', { + routePrerender: route.prerender, + modPrerender: mod.prerender, + }); + console.log({ matchedStaticPath }); + if (!matchedStaticPath && (ssr ? route.prerender : true)) { return GetParamsAndPropsError.NoMatchingStaticPath; } // Note: considered using Object.create(...) for performance @@ -98,8 +104,10 @@ export async function getParamsAndProps( // Replaced with a simple spread as a compromise pageProps = matchedStaticPath?.props ? { ...matchedStaticPath.props } : {}; } else { + console.log('No route!'); pageProps = {}; } + console.log('Finished!'); return [params, pageProps]; } diff --git a/packages/astro/src/core/render/dev/index.ts b/packages/astro/src/core/render/dev/index.ts index 267c6515d31c..fee0f62413d8 100644 --- a/packages/astro/src/core/render/dev/index.ts +++ b/packages/astro/src/core/render/dev/index.ts @@ -21,6 +21,7 @@ import { getStylesForURL } from './css.js'; import type { DevelopmentEnvironment } from './environment'; import { getComponentMetadata } from './metadata.js'; import { getScriptsForURL } from './scripts.js'; +import { getPrerenderStatus } from '../../../prerender/utils.js'; export { createDevelopmentEnvironment } from './environment.js'; export type { DevelopmentEnvironment }; @@ -65,7 +66,12 @@ export async function preload({ try { // Load the module from the Vite SSR Runtime. + console.log('Imported page!'); const mod = (await env.loader.import(fileURLToPath(filePath))) as ComponentInstance; + + console.log('Finished importing page!'); + // we should here set the prerender metadata of the module + return [renderers, mod]; } catch (error) { // If the error came from Markdown or CSS, we already handled it and there's no need to enhance it diff --git a/packages/astro/src/core/render/route-cache.ts b/packages/astro/src/core/render/route-cache.ts index 7f5678669c1d..b96bde0ab297 100644 --- a/packages/astro/src/core/render/route-cache.ts +++ b/packages/astro/src/core/render/route-cache.ts @@ -31,9 +31,13 @@ export async function callGetStaticPaths({ }: CallGetStaticPathsOptions): Promise { validateDynamicRouteModule(mod, { ssr, logging, route }); // No static paths in SSR mode. Return an empty RouteCacheEntry. - if (ssr && !mod.prerender) { - return { staticPaths: Object.assign([], { keyed: new Map() }) }; - } + console.log('callGetStaticPaths', { + routePrerender: route.prerender, + modPrerender: mod.prerender, + }); + // if (ssr && !route.prerender) { + // return { staticPaths: Object.assign([], { keyed: new Map() }) }; + // } // Add a check here to make TypeScript happy. // This is already checked in validateDynamicRouteModule(). if (!mod.getStaticPaths) { diff --git a/packages/astro/src/core/routing/validation.ts b/packages/astro/src/core/routing/validation.ts index 047a5b8923ef..b40e20d3a3d4 100644 --- a/packages/astro/src/core/routing/validation.ts +++ b/packages/astro/src/core/routing/validation.ts @@ -31,10 +31,15 @@ export function validateDynamicRouteModule( route: RouteData; } ) { - if (ssr && mod.getStaticPaths && !mod.prerender) { - warn(logging, 'getStaticPaths', 'getStaticPaths() is ignored when "output: server" is set.'); + console.log('Validation'); + if (ssr && mod.getStaticPaths && !route.prerender) { + console.log( + // logging, + 'getStaticPathss', + 'getStaticPaths() is ignored when "output: server" is set.' + ); } - if ((!ssr || mod.prerender) && !mod.getStaticPaths) { + if ((!ssr || route.prerender) && !mod.getStaticPaths) { throw new AstroError({ ...AstroErrorData.GetStaticPathsRequired, location: { file: route.component }, diff --git a/packages/astro/src/prerender/utils.ts b/packages/astro/src/prerender/utils.ts index 40066035cf66..94db53c864aa 100644 --- a/packages/astro/src/prerender/utils.ts +++ b/packages/astro/src/prerender/utils.ts @@ -1,6 +1,8 @@ // TODO: remove after the experimetal phase when import type { AstroConfig } from '../@types/astro'; +import type { ModuleInfo, ModuleLoader } from '../core/module-loader'; +import { viteID } from '../core/util.js'; export function isHybridMalconfigured(config: AstroConfig) { return config.experimental.hybridOutput ? config.output !== 'hybrid' : config.output === 'hybrid'; @@ -9,3 +11,25 @@ export function isHybridMalconfigured(config: AstroConfig) { export function isHybridOutput(config: AstroConfig) { return config.experimental.hybridOutput && config.output === 'hybrid'; } + +export function getPrerenderMetadata(moduleInfo: ModuleInfo) { + return moduleInfo?.meta?.astro?.pageOptions?.prerender === true; +} + +type GetPrerenderStatusParams = { + filePath: URL; + loader: ModuleLoader; +}; + +export function getPrerenderStatus({ + filePath, + loader, +}: GetPrerenderStatusParams): boolean | undefined { + const fileID = viteID(filePath); + console.log({ fileID }); + const moduleInfo = loader.getModuleInfo(fileID); + if (!moduleInfo) return; + const prerenderStatus = getPrerenderMetadata(moduleInfo); + console.log({ prerenderStatus }); + return prerenderStatus; +} diff --git a/packages/astro/src/vite-plugin-astro-server/route.ts b/packages/astro/src/vite-plugin-astro-server/route.ts index 259acd2eb95e..9d374c52cde0 100644 --- a/packages/astro/src/vite-plugin-astro-server/route.ts +++ b/packages/astro/src/vite-plugin-astro-server/route.ts @@ -16,7 +16,7 @@ import { preload, renderPage } from '../core/render/dev/index.js'; import { getParamsAndProps, GetParamsAndPropsError } from '../core/render/index.js'; import { createRequest } from '../core/request.js'; import { matchAllRoutes } from '../core/routing/index.js'; -import { isHybridOutput } from '../prerender/utils.js'; +import { getPrerenderStatus, isHybridOutput } from '../prerender/utils.js'; import { log404 } from './common.js'; import { handle404Response, writeSSRResult, writeWebResponse } from './response.js'; @@ -50,6 +50,17 @@ export async function matchRoute( for await (const maybeRoute of matches) { const filePath = new URL(`./${maybeRoute.component}`, settings.config.root); const preloadedComponent = await preload({ env, filePath }); + + const prerenderStatus = + getPrerenderStatus({ + filePath, + loader: env.loader, + }) ?? maybeRoute.prerender; + + if (prerenderStatus !== undefined) { + maybeRoute.prerender = prerenderStatus; + } + const [, mod] = preloadedComponent; // attempt to get static paths // if this fails, we have a bad URL match! diff --git a/packages/astro/src/vite-plugin-scanner/scan.ts b/packages/astro/src/vite-plugin-scanner/scan.ts index 7723c3fd6289..f696d0a83079 100644 --- a/packages/astro/src/vite-plugin-scanner/scan.ts +++ b/packages/astro/src/vite-plugin-scanner/scan.ts @@ -35,6 +35,7 @@ function isFalsy(value: string) { let didInit = false; export async function scan(code: string, id: string, isHybridOutput = false): Promise { + console.log('Plugin scanner run!!'); if (!includesExport(code)) return {}; if (!didInit) { await eslexer.init; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 30368328c153..68d43ab76867 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2409,6 +2409,15 @@ importers: specifier: workspace:* version: link:../../.. + packages/astro/test/fixtures/content-ssr-integration-prerender: + dependencies: + '@astrojs/mdx': + specifier: workspace:* + version: link:../../../../integrations/mdx + astro: + specifier: workspace:* + version: link:../../.. + packages/astro/test/fixtures/content-static-paths-integration: dependencies: '@astrojs/mdx': @@ -3278,6 +3287,15 @@ importers: specifier: workspace:* version: link:../../.. + packages/astro/test/fixtures/ssr-hybrid-get-static-paths: + dependencies: + '@astrojs/node': + specifier: '*' + version: link:../../../../integrations/node + astro: + specifier: '*' + version: link:../../.. + packages/astro/test/fixtures/ssr-manifest: dependencies: astro: @@ -5756,7 +5774,7 @@ packages: resolution: {integrity: sha512-kkH7sWzKPq0xt3H1n+ghb4xEMP8k0U7XV3kkB+ZGy69kDk2ySFW1qPi06sjKzFY3t1j6XbJSqr4mF9L7CYVyhg==} engines: {node: '>=6.9.0'} dependencies: - '@babel/types': 7.18.4 + '@babel/types': 7.21.5 dev: false /@babel/helper-module-imports@7.21.4: @@ -5850,7 +5868,6 @@ packages: /@babel/helper-string-parser@7.21.5: resolution: {integrity: sha512-5pTUx3hAJaZIdW99sJ6ZUUgWq/Y+Hja7TowEnLNMm1VivRgZQL3vpBY3qUACVsvw+yQU6+YgfBVmcbLaZtrA1w==} engines: {node: '>=6.9.0'} - dev: false /@babel/helper-validator-identifier@7.19.1: resolution: {integrity: sha512-awrNfaMtnHUr653GgGEs++LlAvW6w+DcPrOliSMXWCKo597CwL5Acf/wWdNkf/tfEQE3mjkeD1YOVZOUV/od1w==} @@ -5904,8 +5921,7 @@ packages: engines: {node: '>=6.0.0'} hasBin: true dependencies: - '@babel/types': 7.18.4 - dev: false + '@babel/types': 7.21.5 /@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression@7.18.6(@babel/core@7.18.2): resolution: {integrity: sha512-Dgxsyg54Fx1d4Nge8UnvTrED63vrwOdPmyvPzlNN/boaliRP54pm3pGzZD1SJUwrBA+Cs/xdG8kXX6Mn/RfISQ==} @@ -6993,7 +7009,7 @@ packages: '@babel/helper-plugin-utils': 7.21.5 '@babel/plugin-proposal-unicode-property-regex': 7.18.6(@babel/core@7.18.2) '@babel/plugin-transform-dotall-regex': 7.18.6(@babel/core@7.18.2) - '@babel/types': 7.18.4 + '@babel/types': 7.21.5 esutils: 2.0.3 dev: false @@ -7066,7 +7082,6 @@ packages: '@babel/helper-string-parser': 7.21.5 '@babel/helper-validator-identifier': 7.19.1 to-fast-properties: 2.0.0 - dev: false /@builder.io/partytown@0.7.4: resolution: {integrity: sha512-dcZBPNQiHbMhvDGmdWRFNe75Z/XYmeZ2bubYmC5BeQpF09ObbPcbSqIP2NaNOFonKlWLfsE6u1790o9ZmlfpIw==} @@ -8717,7 +8732,7 @@ packages: /@ts-morph/common@0.16.0: resolution: {integrity: sha512-SgJpzkTgZKLKqQniCjLaE3c2L2sdL7UShvmTmPBejAKd2OKV/yfMpQ2IWpAuA+VY5wy7PkSUaEObIqEK6afFuw==} dependencies: - fast-glob: 3.2.11 + fast-glob: 3.2.12 minimatch: 5.1.6 mkdirp: 1.0.4 path-browserify: 1.0.1 @@ -9419,7 +9434,7 @@ packages: /@vue/compiler-core@3.2.47: resolution: {integrity: sha512-p4D7FDnQb7+YJmO2iPEv0SQNeNzcbHdGByJDsT4lynf63AFkOTFN07HsiRSvjGo0QrxR/o3d0hUyNCUnBU2Tig==} dependencies: - '@babel/parser': 7.18.4 + '@babel/parser': 7.21.8 '@vue/shared': 3.2.47 estree-walker: 2.0.2 source-map: 0.6.1 @@ -9492,7 +9507,7 @@ packages: /@vue/reactivity-transform@3.2.47: resolution: {integrity: sha512-m8lGXw8rdnPVVIdIFhf0LeQ/ixyHkH5plYuS83yop5n7ggVJU+z5v0zecwEnX7fa7HNLBhh2qngJJkxpwEEmYA==} dependencies: - '@babel/parser': 7.18.4 + '@babel/parser': 7.21.8 '@vue/compiler-core': 3.2.47 '@vue/shared': 3.2.47 estree-walker: 2.0.2 @@ -10124,7 +10139,7 @@ packages: /builtins@5.0.1: resolution: {integrity: sha512-qwVpFEHNfhYJIzNRBvd2C1kyo6jz3ZSMPyyuR47OPdiKWlbYnZNyDWuyR175qDnAJLiCo5fBBqPb3RiXgWlkOQ==} dependencies: - semver: 7.3.8 + semver: 7.5.1 dev: true /bundle-name@3.0.0: @@ -14531,7 +14546,7 @@ packages: resolution: {integrity: sha512-zNy02qivjjRosswoYmPi8hIKJRr8MpQyeKT6qlcq/OnOgA3Rhoae+IYOqsM9V5+JnHWmxKnWOT2GxvtqdtOCXA==} engines: {node: '>=10'} dependencies: - semver: 7.3.8 + semver: 7.5.1 /node-addon-api@6.1.0: resolution: {integrity: sha512-+eawOlIgy680F0kBzPUNFhMZGtJ1YmqM6l4+Crf4IkImjYrO/mqPwRMh352g23uIaQKFItcQ64I7KMaJxHgAVA==} @@ -14665,7 +14680,7 @@ packages: dependencies: execa: 6.1.0 parse-package-name: 1.0.0 - semver: 7.3.8 + semver: 7.5.1 validate-npm-package-name: 4.0.0 dev: true From 70288188550e6c89ce5dbe19b97c709e14066c83 Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Sat, 20 May 2023 17:09:14 +0000 Subject: [PATCH 05/18] bring back edit lost on merge conflict fix --- packages/astro/src/core/routing/validation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/core/routing/validation.ts b/packages/astro/src/core/routing/validation.ts index ff5786d46a68..1a3bab82a2ee 100644 --- a/packages/astro/src/core/routing/validation.ts +++ b/packages/astro/src/core/routing/validation.ts @@ -32,7 +32,7 @@ export function validateDynamicRouteModule( route: RouteData; } ) { - if (ssr && mod.getStaticPaths && !mod.prerender) { + if (ssr && mod.getStaticPaths && !route.prerender) { warn( logging, 'getStaticPaths', From d4aad34321c13e98bafe66b0dddcc8fffe923f14 Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Sat, 20 May 2023 19:36:45 +0000 Subject: [PATCH 06/18] back static paths guard --- packages/astro/src/core/render/route-cache.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/astro/src/core/render/route-cache.ts b/packages/astro/src/core/render/route-cache.ts index b96bde0ab297..b1a5d7810986 100644 --- a/packages/astro/src/core/render/route-cache.ts +++ b/packages/astro/src/core/render/route-cache.ts @@ -31,13 +31,13 @@ export async function callGetStaticPaths({ }: CallGetStaticPathsOptions): Promise { validateDynamicRouteModule(mod, { ssr, logging, route }); // No static paths in SSR mode. Return an empty RouteCacheEntry. - console.log('callGetStaticPaths', { - routePrerender: route.prerender, - modPrerender: mod.prerender, - }); - // if (ssr && !route.prerender) { - // return { staticPaths: Object.assign([], { keyed: new Map() }) }; - // } + // console.log('callGetStaticPaths', { + // routePrerender: route.prerender, + // modPrerender: mod.prerender, + // }); + if (ssr && !route.prerender) { + return { staticPaths: Object.assign([], { keyed: new Map() }) }; + } // Add a check here to make TypeScript happy. // This is already checked in validateDynamicRouteModule(). if (!mod.getStaticPaths) { From 66dc771551ad79736a5da9864c8a5837db6c9182 Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Sat, 20 May 2023 19:38:38 +0000 Subject: [PATCH 07/18] move function to new file to avoid bundling issues --- packages/astro/src/prerender/status.ts | 21 +++++++++++++++++++ packages/astro/src/prerender/utils.ts | 21 +------------------ .../src/vite-plugin-astro-server/route.ts | 3 ++- 3 files changed, 24 insertions(+), 21 deletions(-) create mode 100644 packages/astro/src/prerender/status.ts diff --git a/packages/astro/src/prerender/status.ts b/packages/astro/src/prerender/status.ts new file mode 100644 index 000000000000..8bbe1d1de185 --- /dev/null +++ b/packages/astro/src/prerender/status.ts @@ -0,0 +1,21 @@ +import type { ModuleLoader } from '../core/module-loader'; +import { viteID } from '../core/util.js'; +import { getPrerenderMetadata } from './utils.js'; + +type GetPrerenderStatusParams = { + filePath: URL; + loader: ModuleLoader; +}; + +export function getPrerenderStatus({ + filePath, + loader, +}: GetPrerenderStatusParams): boolean | undefined { + const fileID = viteID(filePath); + // console.log({ fileID }); + const moduleInfo = loader.getModuleInfo(fileID); + if (!moduleInfo) return; + const prerenderStatus = getPrerenderMetadata(moduleInfo); + // console.log({ prerenderStatus }); + return prerenderStatus; +} diff --git a/packages/astro/src/prerender/utils.ts b/packages/astro/src/prerender/utils.ts index 94db53c864aa..c30e0642096a 100644 --- a/packages/astro/src/prerender/utils.ts +++ b/packages/astro/src/prerender/utils.ts @@ -1,8 +1,7 @@ // TODO: remove after the experimetal phase when import type { AstroConfig } from '../@types/astro'; -import type { ModuleInfo, ModuleLoader } from '../core/module-loader'; -import { viteID } from '../core/util.js'; +import type { ModuleInfo } from '../core/module-loader'; export function isHybridMalconfigured(config: AstroConfig) { return config.experimental.hybridOutput ? config.output !== 'hybrid' : config.output === 'hybrid'; @@ -15,21 +14,3 @@ export function isHybridOutput(config: AstroConfig) { export function getPrerenderMetadata(moduleInfo: ModuleInfo) { return moduleInfo?.meta?.astro?.pageOptions?.prerender === true; } - -type GetPrerenderStatusParams = { - filePath: URL; - loader: ModuleLoader; -}; - -export function getPrerenderStatus({ - filePath, - loader, -}: GetPrerenderStatusParams): boolean | undefined { - const fileID = viteID(filePath); - console.log({ fileID }); - const moduleInfo = loader.getModuleInfo(fileID); - if (!moduleInfo) return; - const prerenderStatus = getPrerenderMetadata(moduleInfo); - console.log({ prerenderStatus }); - return prerenderStatus; -} diff --git a/packages/astro/src/vite-plugin-astro-server/route.ts b/packages/astro/src/vite-plugin-astro-server/route.ts index 9d374c52cde0..f6ef8617fc18 100644 --- a/packages/astro/src/vite-plugin-astro-server/route.ts +++ b/packages/astro/src/vite-plugin-astro-server/route.ts @@ -16,9 +16,10 @@ import { preload, renderPage } from '../core/render/dev/index.js'; import { getParamsAndProps, GetParamsAndPropsError } from '../core/render/index.js'; import { createRequest } from '../core/request.js'; import { matchAllRoutes } from '../core/routing/index.js'; -import { getPrerenderStatus, isHybridOutput } from '../prerender/utils.js'; +import { isHybridOutput } from '../prerender/utils.js'; import { log404 } from './common.js'; import { handle404Response, writeSSRResult, writeWebResponse } from './response.js'; +import { getPrerenderStatus } from '../prerender/status.js'; type AsyncReturnType Promise> = T extends ( ...args: any From dd7d1feb5888284fb33e30b320a5c2e3d988a968 Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Sat, 20 May 2023 19:42:50 +0000 Subject: [PATCH 08/18] remove unsued import --- packages/astro/src/core/render/dev/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/astro/src/core/render/dev/index.ts b/packages/astro/src/core/render/dev/index.ts index fee0f62413d8..85fd0c350eb9 100644 --- a/packages/astro/src/core/render/dev/index.ts +++ b/packages/astro/src/core/render/dev/index.ts @@ -21,7 +21,6 @@ import { getStylesForURL } from './css.js'; import type { DevelopmentEnvironment } from './environment'; import { getComponentMetadata } from './metadata.js'; import { getScriptsForURL } from './scripts.js'; -import { getPrerenderStatus } from '../../../prerender/utils.js'; export { createDevelopmentEnvironment } from './environment.js'; export type { DevelopmentEnvironment }; From a0561d079044753d0c0251d6a48a4b1bd7124411 Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Sat, 20 May 2023 19:57:58 +0000 Subject: [PATCH 09/18] debugging cleanup --- packages/astro/src/core/build/generate.ts | 3 --- packages/astro/src/core/build/plugins/plugin-prerender.ts | 6 +----- packages/astro/src/core/render/core.ts | 8 -------- packages/astro/src/core/render/dev/index.ts | 2 -- packages/astro/src/core/render/route-cache.ts | 4 ---- packages/astro/src/prerender/status.ts | 2 -- packages/astro/src/vite-plugin-scanner/scan.ts | 1 - 7 files changed, 1 insertion(+), 25 deletions(-) diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index bbc90ab67e9f..5a9f075c4c8e 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -105,7 +105,6 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn if (ssr) { for (const pageData of eachPageData(internals)) { - console.log('Route prerendering: ', pageData.route.prerender); if (pageData.route.prerender) await generatePage(opts, internals, pageData, ssrEntry, builtPaths); } @@ -199,7 +198,6 @@ async function generatePage( // Get paths for the route, calling getStaticPaths if needed. const paths = await getPathsForRoute(pageData, pageModule, opts, builtPaths); - console.log({ paths }); for (let i = 0; i < paths.length; i++) { const path = paths[i]; @@ -246,7 +244,6 @@ async function getPathsForRoute( debug('build', `├── ${colors.bold(colors.red('✗'))} ${route.component}`); throw err; }); - console.log({ result }); // Save the route cache so it doesn't get called again opts.routeCache.set(route, result); diff --git a/packages/astro/src/core/build/plugins/plugin-prerender.ts b/packages/astro/src/core/build/plugins/plugin-prerender.ts index c21e6ec56876..2d21dc93cab9 100644 --- a/packages/astro/src/core/build/plugins/plugin-prerender.ts +++ b/packages/astro/src/core/build/plugins/plugin-prerender.ts @@ -6,10 +6,7 @@ import type { StaticBuildOptions } from '../types'; import { extendManualChunks } from './util.js'; import { getPrerenderMetadata } from '../../../prerender/utils.js'; -export function vitePluginPrerender( - opts: StaticBuildOptions, - internals: BuildInternals -): VitePlugin { +function vitePluginPrerender(opts: StaticBuildOptions, internals: BuildInternals): VitePlugin { return { name: 'astro:rollup-plugin-prerender', @@ -25,7 +22,6 @@ export function vitePluginPrerender( // prerendered pages should be split into their own chunk // Important: this can't be in the `pages/` directory! if (getPrerenderMetadata(meta.getModuleInfo(id))) { - console.log('Plugin prerender run!'); pageInfo.route.prerender = true; return 'prerender'; } diff --git a/packages/astro/src/core/render/core.ts b/packages/astro/src/core/render/core.ts index 528d9d6964ff..2d6fb088ee27 100644 --- a/packages/astro/src/core/render/core.ts +++ b/packages/astro/src/core/render/core.ts @@ -49,7 +49,6 @@ export async function getParamsAndProps( // Handle dynamic routes let params: Params = {}; let pageProps: Props; - console.log('Looking for route!'); if (route && !route.pathname) { if (route.params.length) { // The RegExp pattern expects a decoded string, but the pathname is encoded @@ -90,11 +89,6 @@ export async function getParamsAndProps( routeCache.set(route, routeCacheEntry); } const matchedStaticPath = findPathItemByKey(routeCacheEntry.staticPaths, params, route); - console.log('getParamsAndProps', { - routePrerender: route.prerender, - modPrerender: mod.prerender, - }); - console.log({ matchedStaticPath }); if (!matchedStaticPath && (ssr ? route.prerender : true)) { return GetParamsAndPropsError.NoMatchingStaticPath; } @@ -104,10 +98,8 @@ export async function getParamsAndProps( // Replaced with a simple spread as a compromise pageProps = matchedStaticPath?.props ? { ...matchedStaticPath.props } : {}; } else { - console.log('No route!'); pageProps = {}; } - console.log('Finished!'); return [params, pageProps]; } diff --git a/packages/astro/src/core/render/dev/index.ts b/packages/astro/src/core/render/dev/index.ts index 85fd0c350eb9..56c4ec74989b 100644 --- a/packages/astro/src/core/render/dev/index.ts +++ b/packages/astro/src/core/render/dev/index.ts @@ -65,10 +65,8 @@ export async function preload({ try { // Load the module from the Vite SSR Runtime. - console.log('Imported page!'); const mod = (await env.loader.import(fileURLToPath(filePath))) as ComponentInstance; - console.log('Finished importing page!'); // we should here set the prerender metadata of the module return [renderers, mod]; diff --git a/packages/astro/src/core/render/route-cache.ts b/packages/astro/src/core/render/route-cache.ts index b1a5d7810986..227928267dae 100644 --- a/packages/astro/src/core/render/route-cache.ts +++ b/packages/astro/src/core/render/route-cache.ts @@ -31,10 +31,6 @@ export async function callGetStaticPaths({ }: CallGetStaticPathsOptions): Promise { validateDynamicRouteModule(mod, { ssr, logging, route }); // No static paths in SSR mode. Return an empty RouteCacheEntry. - // console.log('callGetStaticPaths', { - // routePrerender: route.prerender, - // modPrerender: mod.prerender, - // }); if (ssr && !route.prerender) { return { staticPaths: Object.assign([], { keyed: new Map() }) }; } diff --git a/packages/astro/src/prerender/status.ts b/packages/astro/src/prerender/status.ts index 8bbe1d1de185..d060fc678296 100644 --- a/packages/astro/src/prerender/status.ts +++ b/packages/astro/src/prerender/status.ts @@ -12,10 +12,8 @@ export function getPrerenderStatus({ loader, }: GetPrerenderStatusParams): boolean | undefined { const fileID = viteID(filePath); - // console.log({ fileID }); const moduleInfo = loader.getModuleInfo(fileID); if (!moduleInfo) return; const prerenderStatus = getPrerenderMetadata(moduleInfo); - // console.log({ prerenderStatus }); return prerenderStatus; } diff --git a/packages/astro/src/vite-plugin-scanner/scan.ts b/packages/astro/src/vite-plugin-scanner/scan.ts index f696d0a83079..7723c3fd6289 100644 --- a/packages/astro/src/vite-plugin-scanner/scan.ts +++ b/packages/astro/src/vite-plugin-scanner/scan.ts @@ -35,7 +35,6 @@ function isFalsy(value: string) { let didInit = false; export async function scan(code: string, id: string, isHybridOutput = false): Promise { - console.log('Plugin scanner run!!'); if (!includesExport(code)) return {}; if (!didInit) { await eslexer.init; From b3b7d8986889c774eb9efc78c4faf42a18d113ca Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Sat, 20 May 2023 19:59:26 +0000 Subject: [PATCH 10/18] chore: update fixture's package.json --- .../ssr-hybrid-get-static-paths/package.json | 3 +-- pnpm-lock.yaml | 14 +------------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/package.json b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/package.json index 6847c5caac6c..8abd76d78700 100644 --- a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/package.json +++ b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/package.json @@ -3,7 +3,6 @@ "version": "0.0.0", "private": true, "dependencies": { - "@astrojs/node": "*", - "astro": "*" + "astro": "workspace:*" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fb5a7e11d58a..6c975070a14f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2409,15 +2409,6 @@ importers: specifier: workspace:* version: link:../../.. - packages/astro/test/fixtures/content-ssr-integration-prerender: - dependencies: - '@astrojs/mdx': - specifier: workspace:* - version: link:../../../../integrations/mdx - astro: - specifier: workspace:* - version: link:../../.. - packages/astro/test/fixtures/content-static-paths-integration: dependencies: '@astrojs/mdx': @@ -3289,11 +3280,8 @@ importers: packages/astro/test/fixtures/ssr-hybrid-get-static-paths: dependencies: - '@astrojs/node': - specifier: '*' - version: link:../../../../integrations/node astro: - specifier: '*' + specifier: workspace:* version: link:../../.. packages/astro/test/fixtures/ssr-manifest: From 86f1d6a707cfe4a79f882a8e034b5c920922bffa Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Sat, 20 May 2023 19:59:37 +0000 Subject: [PATCH 11/18] cleanup test --- .../ssr-prerender-get-static-paths.test.js | 21 ++----------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/packages/astro/test/ssr-prerender-get-static-paths.test.js b/packages/astro/test/ssr-prerender-get-static-paths.test.js index 8f9ecdacaadb..a7ea675b4d55 100644 --- a/packages/astro/test/ssr-prerender-get-static-paths.test.js +++ b/packages/astro/test/ssr-prerender-get-static-paths.test.js @@ -13,18 +13,12 @@ describe('Prerender', () => { describe('getStaticPaths - build calls', () => { /** @type {import('./test-utils').Fixture} */ let fixture; - /** @type {Error} */ - let error; before(async () => { fixture = await loadFixture({ root: './fixtures/ssr-prerender-get-static-paths/', ...sharedConfig, }); - try { - await fixture.build(); - } catch (err) { - error = err; - } + await fixture.build(); }); afterEach(() => { @@ -33,8 +27,6 @@ describe('Prerender', () => { }); it('is only called once during build', () => { - console.log({ error }); - if (error) throw error; // useless expect; if build() throws in setup then this test fails expect(true).to.equal(true); }); @@ -163,19 +155,12 @@ describe('Hybrid', () => { /** @type {import('./test-utils').Fixture} */ let fixture; - /** @type {Error} */ - let error; - before(async () => { fixture = await loadFixture({ root: './fixtures/ssr-hybrid-get-static-paths/', ...sharedConfig, }); - try { - await fixture.build(); - } catch (err) { - error = err; - } + await fixture.build(); }); afterEach(() => { @@ -184,8 +169,6 @@ describe('Hybrid', () => { }); it('is only called once during build', () => { - console.log({ error }); - if (error) throw error; // useless expect; if build() throws in setup then this test fails expect(true).to.equal(true); }); From 7e6f02f99abb7051192deaa5b6f005a0d5ec7c9a Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Sat, 20 May 2023 20:04:43 +0000 Subject: [PATCH 12/18] small test refactoring --- .../ssr-prerender-get-static-paths.test.js | 447 +++++++++--------- 1 file changed, 223 insertions(+), 224 deletions(-) diff --git a/packages/astro/test/ssr-prerender-get-static-paths.test.js b/packages/astro/test/ssr-prerender-get-static-paths.test.js index a7ea675b4d55..51eea987dee3 100644 --- a/packages/astro/test/ssr-prerender-get-static-paths.test.js +++ b/packages/astro/test/ssr-prerender-get-static-paths.test.js @@ -4,280 +4,279 @@ import * as cheerio from 'cheerio'; import testAdapter from './test-adapter.js'; describe('Prerender', () => { - const sharedConfig = { - site: 'https://mysite.dev/', - adapter: testAdapter(), - base: '/blog', - output: 'server', - }; - describe('getStaticPaths - build calls', () => { - /** @type {import('./test-utils').Fixture} */ - let fixture; - before(async () => { - fixture = await loadFixture({ - root: './fixtures/ssr-prerender-get-static-paths/', - ...sharedConfig, + describe('output: "server"', () => { + const sharedConfig = { + site: 'https://mysite.dev/', + adapter: testAdapter(), + base: '/blog', + output: 'server', + }; + describe('getStaticPaths - build calls', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + before(async () => { + fixture = await loadFixture({ + root: './fixtures/ssr-prerender-get-static-paths/', + ...sharedConfig, + }); + await fixture.build(); }); - await fixture.build(); - }); - - afterEach(() => { - // reset the flag used by [...calledTwiceTest].astro between each test - globalThis.isCalledOnce = false; - }); - - it('is only called once during build', () => { - // useless expect; if build() throws in setup then this test fails - expect(true).to.equal(true); - }); - it('Astro.url sets the current pathname', async () => { - const html = await fixture.readFile('/client/food/tacos/index.html'); - const $ = cheerio.load(html); + afterEach(() => { + // reset the flag used by [...calledTwiceTest].astro between each test + globalThis.isCalledOnce = false; + }); - expect($('#props').text()).to.equal('10'); - expect($('#url').text()).to.equal('/blog/food/tacos/'); - }); - }); + it('is only called once during build', () => { + // useless expect; if build() throws in setup then this test fails + expect(true).to.equal(true); + }); - describe('getStaticPaths - dev calls', () => { - /** @type {import('./test-utils').Fixture} */ - let fixture; - let devServer; + it('Astro.url sets the current pathname', async () => { + const html = await fixture.readFile('/client/food/tacos/index.html'); + const $ = cheerio.load(html); - before(async () => { - globalThis.isCalledOnce = false; - fixture = await loadFixture({ - root: './fixtures/ssr-prerender-get-static-paths/', - ...sharedConfig, + expect($('#props').text()).to.equal('10'); + expect($('#url').text()).to.equal('/blog/food/tacos/'); }); - devServer = await fixture.startDevServer(); }); - afterEach(() => { - // reset the flag used by [...calledTwiceTest].astro between each test - globalThis.isCalledOnce = false; - }); + describe('getStaticPaths - dev calls', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + let devServer; + + before(async () => { + globalThis.isCalledOnce = false; + fixture = await loadFixture({ + root: './fixtures/ssr-prerender-get-static-paths/', + ...sharedConfig, + }); + devServer = await fixture.startDevServer(); + }); - after(async () => { - devServer.stop(); - }); + afterEach(() => { + // reset the flag used by [...calledTwiceTest].astro between each test + globalThis.isCalledOnce = false; + }); - it('only calls prerender getStaticPaths once', async () => { - let res = await fixture.fetch('/blog/a'); - expect(res.status).to.equal(200); + after(async () => { + devServer.stop(); + }); - res = await fixture.fetch('/blog/b'); - expect(res.status).to.equal(200); + it('only calls prerender getStaticPaths once', async () => { + let res = await fixture.fetch('/blog/a'); + expect(res.status).to.equal(200); - res = await fixture.fetch('/blog/c'); - expect(res.status).to.equal(200); - }); + res = await fixture.fetch('/blog/b'); + expect(res.status).to.equal(200); - describe('404 behavior', () => { - it('resolves 200 on matching static path - named params', async () => { - const res = await fixture.fetch('/blog/pizza/provolone-sausage'); + res = await fixture.fetch('/blog/c'); expect(res.status).to.equal(200); }); - it('resolves 404 on pattern match without static path - named params', async () => { - const res = await fixture.fetch('/blog/pizza/provolone-pineapple'); - const html = await res.text(); - console.log({ html }); - expect(res.status).to.equal(404); - expect(html).to.match(/404/); + describe('404 behavior', () => { + it('resolves 200 on matching static path - named params', async () => { + const res = await fixture.fetch('/blog/pizza/provolone-sausage'); + expect(res.status).to.equal(200); + }); + + it('resolves 404 on pattern match without static path - named params', async () => { + const res = await fixture.fetch('/blog/pizza/provolone-pineapple'); + const html = await res.text(); + expect(res.status).to.equal(404); + expect(html).to.match(/404/); + }); + + it('resolves 200 on matching static path - rest params', async () => { + const res = await fixture.fetch('/blog/pizza/grimaldis/new-york'); + expect(res.status).to.equal(200); + }); + + it('resolves 404 on pattern match without static path - rest params', async () => { + const res = await fixture.fetch('/blog/pizza/pizza-hut'); + const html = await res.text(); + + expect(res.status).to.equal(404); + expect(html).to.match(/404/); + }); }); - it('resolves 200 on matching static path - rest params', async () => { - const res = await fixture.fetch('/blog/pizza/grimaldis/new-york'); - expect(res.status).to.equal(200); + describe('route params type validation', () => { + it('resolves 200 on nested array parameters', async () => { + const res = await fixture.fetch('/blog/nested-arrays/slug1'); + expect(res.status).to.equal(200); + }); + + it('resolves 200 on matching static path - string params', async () => { + // route provided with { params: { year: "2022", slug: "post-2" }} + const res = await fixture.fetch('/blog/blog/2022/post-1'); + expect(res.status).to.equal(200); + }); + + it('resolves 200 on matching static path - numeric params', async () => { + // route provided with { params: { year: 2022, slug: "post-2" }} + const res = await fixture.fetch('/blog/blog/2022/post-2'); + expect(res.status).to.equal(200); + }); }); - it('resolves 404 on pattern match without static path - rest params', async () => { - const res = await fixture.fetch('/blog/pizza/pizza-hut'); - const html = await res.text(); - console.log({ html }); - - expect(res.status).to.equal(404); - expect(html).to.match(/404/); + it('resolves 200 on matching static paths', async () => { + // routes params provided for pages /posts/1, /posts/2, and /posts/3 + for (const page of [1, 2, 3]) { + let res = await fixture.fetch(`/blog/posts/${page}`); + expect(res.status).to.equal(200); + + const html = await res.text(); + const $ = cheerio.load(html); + + const canonical = $('link[rel=canonical]'); + expect(canonical.attr('href')).to.equal( + `https://mysite.dev/blog/posts/${page}`, + `doesn't trim the /${page} route param` + ); + } }); }); + }); - describe('route params type validation', () => { - it('resolves 200 on nested array parameters', async () => { - const res = await fixture.fetch('/blog/nested-arrays/slug1'); - expect(res.status).to.equal(200); + describe('output: "hybrid"', () => { + const sharedConfig = { + site: 'https://mysite.dev/', + adapter: testAdapter(), + base: '/blog', + output: 'hybrid', + experimental: { + hybridOutput: true, + }, + }; + describe('getStaticPaths - build calls', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/ssr-hybrid-get-static-paths/', + ...sharedConfig, + }); + await fixture.build(); }); - it('resolves 200 on matching static path - string params', async () => { - // route provided with { params: { year: "2022", slug: "post-2" }} - const res = await fixture.fetch('/blog/blog/2022/post-1'); - expect(res.status).to.equal(200); + afterEach(() => { + // reset the flag used by [...calledTwiceTest].astro between each test + globalThis.isCalledOnce = false; }); - it('resolves 200 on matching static path - numeric params', async () => { - // route provided with { params: { year: 2022, slug: "post-2" }} - const res = await fixture.fetch('/blog/blog/2022/post-2'); - expect(res.status).to.equal(200); + it('is only called once during build', () => { + // useless expect; if build() throws in setup then this test fails + expect(true).to.equal(true); }); - }); - - it('resolves 200 on matching static paths', async () => { - // routes params provided for pages /posts/1, /posts/2, and /posts/3 - for (const page of [1, 2, 3]) { - let res = await fixture.fetch(`/blog/posts/${page}`); - expect(res.status).to.equal(200); - const html = await res.text(); + it('Astro.url sets the current pathname', async () => { + const html = await fixture.readFile('/client/food/tacos/index.html'); const $ = cheerio.load(html); - const canonical = $('link[rel=canonical]'); - expect(canonical.attr('href')).to.equal( - `https://mysite.dev/blog/posts/${page}`, - `doesn't trim the /${page} route param` - ); - } - }); - }); -}); - -describe('Hybrid', () => { - const sharedConfig = { - site: 'https://mysite.dev/', - adapter: testAdapter(), - base: '/blog', - output: 'hybrid', - experimental: { - hybridOutput: true, - }, - }; - describe('getStaticPaths - build calls', () => { - /** @type {import('./test-utils').Fixture} */ - let fixture; - - before(async () => { - fixture = await loadFixture({ - root: './fixtures/ssr-hybrid-get-static-paths/', - ...sharedConfig, + expect($('#props').text()).to.equal('10'); + expect($('#url').text()).to.equal('/blog/food/tacos/'); }); - await fixture.build(); }); - afterEach(() => { - // reset the flag used by [...calledTwiceTest].astro between each test - globalThis.isCalledOnce = false; - }); - - it('is only called once during build', () => { - // useless expect; if build() throws in setup then this test fails - expect(true).to.equal(true); - }); - - it('Astro.url sets the current pathname', async () => { - const html = await fixture.readFile('/client/food/tacos/index.html'); - const $ = cheerio.load(html); - - expect($('#props').text()).to.equal('10'); - expect($('#url').text()).to.equal('/blog/food/tacos/'); - }); - }); - - describe('getStaticPaths - dev calls', () => { - let fixture; - let devServer; - - before(async () => { - globalThis.isCalledOnce = false; - fixture = await loadFixture({ - root: './fixtures/ssr-hybrid-get-static-paths/', - ...sharedConfig, + describe('getStaticPaths - dev calls', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + let devServer; + + before(async () => { + globalThis.isCalledOnce = false; + fixture = await loadFixture({ + root: './fixtures/ssr-hybrid-get-static-paths/', + ...sharedConfig, + }); + devServer = await fixture.startDevServer(); }); - devServer = await fixture.startDevServer(); - }); - - afterEach(() => { - // reset the flag used by [...calledTwiceTest].astro between each test - globalThis.isCalledOnce = false; - }); - - after(async () => { - devServer.stop(); - }); - - it('only calls hybrid getStaticPaths once', async () => { - let res = await fixture.fetch('/blog/a'); - expect(res.status).to.equal(200); - res = await fixture.fetch('/blog/b'); - expect(res.status).to.equal(200); - - res = await fixture.fetch('/blog/c'); - expect(res.status).to.equal(200); - }); - - describe('404 behavior', () => { - it('resolves 200 on matching static path - named params', async () => { - const res = await fixture.fetch('/blog/pizza/provolone-sausage'); - expect(res.status).to.equal(200); + afterEach(() => { + // reset the flag used by [...calledTwiceTest].astro between each test + globalThis.isCalledOnce = false; }); - it('resolves 404 on pattern match without static path - named params', async () => { - const res = await fixture.fetch('/blog/pizza/provolone-pineapple'); - const html = await res.text(); - console.log({ html }); - expect(res.status).to.equal(404); - expect(html).to.match(/404/); + after(async () => { + devServer.stop(); }); - it('resolves 200 on matching static path - rest params', async () => { - const res = await fixture.fetch('/blog/pizza/grimaldis/new-york'); + it('only calls hybrid getStaticPaths once', async () => { + let res = await fixture.fetch('/blog/a'); expect(res.status).to.equal(200); - }); - - it('resolves 404 on pattern match without static path - rest params', async () => { - const res = await fixture.fetch('/blog/pizza/pizza-hut'); - const html = await res.text(); - console.log({ html }); - expect(res.status).to.equal(404); - expect(html).to.match(/404/); - }); - }); - - describe('route params type validation', () => { - it('resolves 200 on nested array parameters', async () => { - const res = await fixture.fetch('/blog/nested-arrays/slug1'); + res = await fixture.fetch('/blog/b'); expect(res.status).to.equal(200); - }); - it('resolves 200 on matching static path - string params', async () => { - // route provided with { params: { year: "2022", slug: "post-2" }} - const res = await fixture.fetch('/blog/blog/2022/post-1'); + res = await fixture.fetch('/blog/c'); expect(res.status).to.equal(200); }); - it('resolves 200 on matching static path - numeric params', async () => { - // route provided with { params: { year: 2022, slug: "post-2" }} - const res = await fixture.fetch('/blog/blog/2022/post-2'); - expect(res.status).to.equal(200); + describe('404 behavior', () => { + it('resolves 200 on matching static path - named params', async () => { + const res = await fixture.fetch('/blog/pizza/provolone-sausage'); + expect(res.status).to.equal(200); + }); + + it('resolves 404 on pattern match without static path - named params', async () => { + const res = await fixture.fetch('/blog/pizza/provolone-pineapple'); + const html = await res.text(); + expect(res.status).to.equal(404); + expect(html).to.match(/404/); + }); + + it('resolves 200 on matching static path - rest params', async () => { + const res = await fixture.fetch('/blog/pizza/grimaldis/new-york'); + expect(res.status).to.equal(200); + }); + + it('resolves 404 on pattern match without static path - rest params', async () => { + const res = await fixture.fetch('/blog/pizza/pizza-hut'); + const html = await res.text(); + + expect(res.status).to.equal(404); + expect(html).to.match(/404/); + }); }); - }); - - it('resolves 200 on matching static paths', async () => { - // routes params provided for pages /posts/1, /posts/2, and /posts/3 - for (const page of [1, 2, 3]) { - let res = await fixture.fetch(`/blog/posts/${page}`); - expect(res.status).to.equal(200); - const html = await res.text(); - const $ = cheerio.load(html); + describe('route params type validation', () => { + it('resolves 200 on nested array parameters', async () => { + const res = await fixture.fetch('/blog/nested-arrays/slug1'); + expect(res.status).to.equal(200); + }); + + it('resolves 200 on matching static path - string params', async () => { + // route provided with { params: { year: "2022", slug: "post-2" }} + const res = await fixture.fetch('/blog/blog/2022/post-1'); + expect(res.status).to.equal(200); + }); + + it('resolves 200 on matching static path - numeric params', async () => { + // route provided with { params: { year: 2022, slug: "post-2" }} + const res = await fixture.fetch('/blog/blog/2022/post-2'); + expect(res.status).to.equal(200); + }); + }); - const canonical = $('link[rel=canonical]'); - expect(canonical.attr('href')).to.equal( - `https://mysite.dev/blog/posts/${page}`, - `doesn't trim the /${page} route param` - ); - } + it('resolves 200 on matching static paths', async () => { + // routes params provided for pages /posts/1, /posts/2, and /posts/3 + for (const page of [1, 2, 3]) { + let res = await fixture.fetch(`/blog/posts/${page}`); + expect(res.status).to.equal(200); + + const html = await res.text(); + const $ = cheerio.load(html); + + const canonical = $('link[rel=canonical]'); + expect(canonical.attr('href')).to.equal( + `https://mysite.dev/blog/posts/${page}`, + `doesn't trim the /${page} route param` + ); + } + }); }); }); }); From de595e65032823eb50dc42a56fa41a7c4109ad48 Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Sat, 20 May 2023 21:08:52 +0000 Subject: [PATCH 13/18] `status.ts` --> `metadata.ts` --- packages/astro/src/core/build/plugins/plugin-prerender.ts | 2 +- packages/astro/src/core/render/dev/index.ts | 2 -- packages/astro/src/prerender/{status.ts => metadata.ts} | 7 +++++-- packages/astro/src/prerender/utils.ts | 5 ----- packages/astro/src/vite-plugin-astro-server/route.ts | 2 +- 5 files changed, 7 insertions(+), 11 deletions(-) rename packages/astro/src/prerender/{status.ts => metadata.ts} (67%) diff --git a/packages/astro/src/core/build/plugins/plugin-prerender.ts b/packages/astro/src/core/build/plugins/plugin-prerender.ts index 2d21dc93cab9..b950a83ea5ac 100644 --- a/packages/astro/src/core/build/plugins/plugin-prerender.ts +++ b/packages/astro/src/core/build/plugins/plugin-prerender.ts @@ -4,7 +4,7 @@ import type { BuildInternals } from '../internal.js'; import type { AstroBuildPlugin } from '../plugin.js'; import type { StaticBuildOptions } from '../types'; import { extendManualChunks } from './util.js'; -import { getPrerenderMetadata } from '../../../prerender/utils.js'; +import { getPrerenderMetadata } from '../../../prerender/metadata.js'; function vitePluginPrerender(opts: StaticBuildOptions, internals: BuildInternals): VitePlugin { return { diff --git a/packages/astro/src/core/render/dev/index.ts b/packages/astro/src/core/render/dev/index.ts index 56c4ec74989b..26e7c85d53b5 100644 --- a/packages/astro/src/core/render/dev/index.ts +++ b/packages/astro/src/core/render/dev/index.ts @@ -67,8 +67,6 @@ export async function preload({ // Load the module from the Vite SSR Runtime. const mod = (await env.loader.import(fileURLToPath(filePath))) as ComponentInstance; - // we should here set the prerender metadata of the module - return [renderers, mod]; } catch (error) { // If the error came from Markdown or CSS, we already handled it and there's no need to enhance it diff --git a/packages/astro/src/prerender/status.ts b/packages/astro/src/prerender/metadata.ts similarity index 67% rename from packages/astro/src/prerender/status.ts rename to packages/astro/src/prerender/metadata.ts index d060fc678296..8ce4df014ead 100644 --- a/packages/astro/src/prerender/status.ts +++ b/packages/astro/src/prerender/metadata.ts @@ -1,6 +1,5 @@ -import type { ModuleLoader } from '../core/module-loader'; +import type { ModuleInfo, ModuleLoader } from '../core/module-loader'; import { viteID } from '../core/util.js'; -import { getPrerenderMetadata } from './utils.js'; type GetPrerenderStatusParams = { filePath: URL; @@ -17,3 +16,7 @@ export function getPrerenderStatus({ const prerenderStatus = getPrerenderMetadata(moduleInfo); return prerenderStatus; } + +export function getPrerenderMetadata(moduleInfo: ModuleInfo) { + return moduleInfo?.meta?.astro?.pageOptions?.prerender === true; +} diff --git a/packages/astro/src/prerender/utils.ts b/packages/astro/src/prerender/utils.ts index c30e0642096a..40066035cf66 100644 --- a/packages/astro/src/prerender/utils.ts +++ b/packages/astro/src/prerender/utils.ts @@ -1,7 +1,6 @@ // TODO: remove after the experimetal phase when import type { AstroConfig } from '../@types/astro'; -import type { ModuleInfo } from '../core/module-loader'; export function isHybridMalconfigured(config: AstroConfig) { return config.experimental.hybridOutput ? config.output !== 'hybrid' : config.output === 'hybrid'; @@ -10,7 +9,3 @@ export function isHybridMalconfigured(config: AstroConfig) { export function isHybridOutput(config: AstroConfig) { return config.experimental.hybridOutput && config.output === 'hybrid'; } - -export function getPrerenderMetadata(moduleInfo: ModuleInfo) { - return moduleInfo?.meta?.astro?.pageOptions?.prerender === true; -} diff --git a/packages/astro/src/vite-plugin-astro-server/route.ts b/packages/astro/src/vite-plugin-astro-server/route.ts index f6ef8617fc18..5cd7da667a10 100644 --- a/packages/astro/src/vite-plugin-astro-server/route.ts +++ b/packages/astro/src/vite-plugin-astro-server/route.ts @@ -19,7 +19,7 @@ import { matchAllRoutes } from '../core/routing/index.js'; import { isHybridOutput } from '../prerender/utils.js'; import { log404 } from './common.js'; import { handle404Response, writeSSRResult, writeWebResponse } from './response.js'; -import { getPrerenderStatus } from '../prerender/status.js'; +import { getPrerenderStatus } from '../prerender/metadata.js'; type AsyncReturnType Promise> = T extends ( ...args: any From ac289c65e85d3078645a2f1b312ea0fb0461cb1a Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Sat, 20 May 2023 21:15:30 +0000 Subject: [PATCH 14/18] smol refactor --- packages/astro/src/vite-plugin-astro-server/route.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/astro/src/vite-plugin-astro-server/route.ts b/packages/astro/src/vite-plugin-astro-server/route.ts index 5cd7da667a10..2f6e3259e9b6 100644 --- a/packages/astro/src/vite-plugin-astro-server/route.ts +++ b/packages/astro/src/vite-plugin-astro-server/route.ts @@ -52,11 +52,11 @@ export async function matchRoute( const filePath = new URL(`./${maybeRoute.component}`, settings.config.root); const preloadedComponent = await preload({ env, filePath }); - const prerenderStatus = - getPrerenderStatus({ - filePath, - loader: env.loader, - }) ?? maybeRoute.prerender; + // gets the prerender metadata set by the `astro:scanner` vite plugin + const prerenderStatus = getPrerenderStatus({ + filePath, + loader: env.loader, + }); if (prerenderStatus !== undefined) { maybeRoute.prerender = prerenderStatus; From dd3b2e5aec2ec224b29b68efa80771c265775023 Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Sat, 20 May 2023 21:36:40 +0000 Subject: [PATCH 15/18] chore: changeset --- .changeset/strange-ties-cry.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/strange-ties-cry.md diff --git a/.changeset/strange-ties-cry.md b/.changeset/strange-ties-cry.md new file mode 100644 index 000000000000..0c6139cfcba7 --- /dev/null +++ b/.changeset/strange-ties-cry.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +fix no matched path when using `getStaticPaths` without `prerender` export. From de83451297408207cd37d81bc9c1a38a942dfdac Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Sun, 21 May 2023 19:16:58 +0000 Subject: [PATCH 16/18] just return the prerender metadata --- packages/astro/src/prerender/metadata.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/prerender/metadata.ts b/packages/astro/src/prerender/metadata.ts index 8ce4df014ead..15527af37aaa 100644 --- a/packages/astro/src/prerender/metadata.ts +++ b/packages/astro/src/prerender/metadata.ts @@ -18,5 +18,5 @@ export function getPrerenderStatus({ } export function getPrerenderMetadata(moduleInfo: ModuleInfo) { - return moduleInfo?.meta?.astro?.pageOptions?.prerender === true; + return moduleInfo?.meta?.astro?.pageOptions?.prerender; } From c6d6fea776a978e2b8bc474b238184c1701faf7d Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Mon, 22 May 2023 11:58:38 +0000 Subject: [PATCH 17/18] refactor tests --- .../ssr-hybrid-get-static-paths/package.json | 8 -- .../src/pages/[...calledTwiceTest].astro | 21 ----- .../src/pages/blog/[year]/[slug].astro | 18 ----- .../src/pages/data/[slug].json.ts | 14 ---- .../src/pages/food/[name].astro | 32 -------- .../src/pages/nested-arrays/[slug].astro | 9 --- .../src/pages/pizza/[...pizza].astro | 22 ------ .../src/pages/pizza/[cheese]-[topping].astro | 21 ----- .../src/pages/posts/[page].astro | 29 ------- .../ssr-prerender-get-static-paths.test.js | 79 +++++++++++-------- 10 files changed, 44 insertions(+), 209 deletions(-) delete mode 100644 packages/astro/test/fixtures/ssr-hybrid-get-static-paths/package.json delete mode 100644 packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/[...calledTwiceTest].astro delete mode 100644 packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/blog/[year]/[slug].astro delete mode 100644 packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/data/[slug].json.ts delete mode 100644 packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/food/[name].astro delete mode 100644 packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/nested-arrays/[slug].astro delete mode 100644 packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/pizza/[...pizza].astro delete mode 100644 packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/pizza/[cheese]-[topping].astro delete mode 100644 packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/posts/[page].astro diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/package.json b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/package.json deleted file mode 100644 index 8abd76d78700..000000000000 --- a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/package.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "name": "@test/ssr-hybrid-get-static-paths", - "version": "0.0.0", - "private": true, - "dependencies": { - "astro": "workspace:*" - } -} diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/[...calledTwiceTest].astro b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/[...calledTwiceTest].astro deleted file mode 100644 index 08b6af30c085..000000000000 --- a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/[...calledTwiceTest].astro +++ /dev/null @@ -1,21 +0,0 @@ ---- -export function getStaticPaths({ paginate }) { - if (globalThis.isCalledOnce) { - throw new Error("Can only be called once!"); - } - globalThis.isCalledOnce = true; - return [ - {params: {calledTwiceTest: 'a'}}, - {params: {calledTwiceTest: 'b'}}, - {params: {calledTwiceTest: 'c'}}, - ]; -} -const { params } = Astro; ---- - - - - Page {params.calledTwiceTest} - - - diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/blog/[year]/[slug].astro b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/blog/[year]/[slug].astro deleted file mode 100644 index 95902cf07b7e..000000000000 --- a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/blog/[year]/[slug].astro +++ /dev/null @@ -1,18 +0,0 @@ ---- -export async function getStaticPaths() { - return [ - { params: { year: '2022', slug: 'post-1' } }, - { params: { year: 2022, slug: 'post-2' } }, - { params: { slug: 'post-2', year: '2022' } }, - ] -} - -const { year, slug } = Astro.params ---- - - - - {year} | {slug} - - - diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/data/[slug].json.ts b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/data/[slug].json.ts deleted file mode 100644 index 3c7cc63baa00..000000000000 --- a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/data/[slug].json.ts +++ /dev/null @@ -1,14 +0,0 @@ -export async function getStaticPaths() { - return [ - { params: { slug: 'thing1' } }, - { params: { slug: 'thing2' } } - ]; -} - -export async function get() { - return { - body: JSON.stringify({ - title: '[slug]' - }, null, 4) - }; -} diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/food/[name].astro b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/food/[name].astro deleted file mode 100644 index e50ab3654e05..000000000000 --- a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/food/[name].astro +++ /dev/null @@ -1,32 +0,0 @@ ---- -export async function getStaticPaths() { - return [ - { - params: { name: 'tacos' }, - props: { yum: 10 }, - }, - { - params: { name: 'potatoes' }, - props: { yum: 7 }, - }, - { - params: { name: 'spaghetti' }, - props: { yum: 5 }, - } - ] -} - -const { yum } = Astro.props; ---- - - - - - - Food - - -

{ Astro.url.pathname }

-

{ yum }

- - diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/nested-arrays/[slug].astro b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/nested-arrays/[slug].astro deleted file mode 100644 index 0282fb139a2a..000000000000 --- a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/nested-arrays/[slug].astro +++ /dev/null @@ -1,9 +0,0 @@ ---- - export function getStaticPaths() { - return [ - [ { params: {slug: "slug1"} } ], - [ { params: {slug: "slug2"} } ], - ] - } - ---- diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/pizza/[...pizza].astro b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/pizza/[...pizza].astro deleted file mode 100644 index a58b314e36e4..000000000000 --- a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/pizza/[...pizza].astro +++ /dev/null @@ -1,22 +0,0 @@ ---- -export function getStaticPaths() { - return [{ - params: { pizza: 'papa-johns' }, - }, { - params: { pizza: 'dominos' }, - }, { - params: { pizza: 'grimaldis/new-york' }, - }] -} -const { pizza } = Astro.params ---- - - - - - {pizza ?? 'The landing page'} - - -

Welcome to {pizza ?? 'The landing page'}

- - diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/pizza/[cheese]-[topping].astro b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/pizza/[cheese]-[topping].astro deleted file mode 100644 index a698a76d784a..000000000000 --- a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/pizza/[cheese]-[topping].astro +++ /dev/null @@ -1,21 +0,0 @@ ---- -export function getStaticPaths() { - return [{ - params: { cheese: 'mozzarella', topping: 'pepperoni' }, - }, { - params: { cheese: 'provolone', topping: 'sausage' }, - }] -} -const { cheese, topping } = Astro.params ---- - - - - - {cheese} - - -

🍕 It's pizza time

-

{cheese}-{topping}

- - diff --git a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/posts/[page].astro b/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/posts/[page].astro deleted file mode 100644 index c00edc2f5196..000000000000 --- a/packages/astro/test/fixtures/ssr-hybrid-get-static-paths/src/pages/posts/[page].astro +++ /dev/null @@ -1,29 +0,0 @@ ---- -export async function getStaticPaths() { - return [ - { - params: { page: 1 }, - }, - { - params: { page: 2 }, - }, - { - params: { page: 3 } - } - ] -}; -const { page } = Astro.params -const canonicalURL = new URL(Astro.url.pathname, Astro.site); ---- - - - - - - Posts Page {page} - - - -

Welcome to page {page}

- - diff --git a/packages/astro/test/ssr-prerender-get-static-paths.test.js b/packages/astro/test/ssr-prerender-get-static-paths.test.js index 51eea987dee3..1a1712304168 100644 --- a/packages/astro/test/ssr-prerender-get-static-paths.test.js +++ b/packages/astro/test/ssr-prerender-get-static-paths.test.js @@ -4,24 +4,25 @@ import * as cheerio from 'cheerio'; import testAdapter from './test-adapter.js'; describe('Prerender', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; describe('output: "server"', () => { - const sharedConfig = { - site: 'https://mysite.dev/', - adapter: testAdapter(), - base: '/blog', - output: 'server', - }; describe('getStaticPaths - build calls', () => { - /** @type {import('./test-utils').Fixture} */ - let fixture; before(async () => { fixture = await loadFixture({ root: './fixtures/ssr-prerender-get-static-paths/', - ...sharedConfig, + site: 'https://mysite.dev/', + adapter: testAdapter(), + base: '/blog', + output: 'server', }); await fixture.build(); }); + after(async () => { + await fixture.clean(); + }); + afterEach(() => { // reset the flag used by [...calledTwiceTest].astro between each test globalThis.isCalledOnce = false; @@ -42,16 +43,10 @@ describe('Prerender', () => { }); describe('getStaticPaths - dev calls', () => { - /** @type {import('./test-utils').Fixture} */ - let fixture; let devServer; before(async () => { globalThis.isCalledOnce = false; - fixture = await loadFixture({ - root: './fixtures/ssr-prerender-get-static-paths/', - ...sharedConfig, - }); devServer = await fixture.startDevServer(); }); @@ -141,27 +136,28 @@ describe('Prerender', () => { }); describe('output: "hybrid"', () => { - const sharedConfig = { - site: 'https://mysite.dev/', - adapter: testAdapter(), - base: '/blog', - output: 'hybrid', - experimental: { - hybridOutput: true, - }, - }; describe('getStaticPaths - build calls', () => { - /** @type {import('./test-utils').Fixture} */ - let fixture; - before(async () => { fixture = await loadFixture({ - root: './fixtures/ssr-hybrid-get-static-paths/', - ...sharedConfig, + root: './fixtures/ssr-prerender-get-static-paths/', + site: 'https://mysite.dev/', + adapter: testAdapter(), + base: '/blog', + output: 'hybrid', + experimental: { + hybridOutput: true, + }, + vite: { + plugins: [vitePluginRemovePrerenderExport()], + }, }); await fixture.build(); }); + after(async () => { + await fixture.clean(); + }); + afterEach(() => { // reset the flag used by [...calledTwiceTest].astro between each test globalThis.isCalledOnce = false; @@ -182,16 +178,10 @@ describe('Prerender', () => { }); describe('getStaticPaths - dev calls', () => { - /** @type {import('./test-utils').Fixture} */ - let fixture; let devServer; before(async () => { globalThis.isCalledOnce = false; - fixture = await loadFixture({ - root: './fixtures/ssr-hybrid-get-static-paths/', - ...sharedConfig, - }); devServer = await fixture.startDevServer(); }); @@ -280,3 +270,22 @@ describe('Prerender', () => { }); }); }); + +/** @returns {import('vite').Plugin} */ +function vitePluginRemovePrerenderExport() { + const EXTENSIONS = ['.astro', '.ts']; + /** @type {import('vite').Plugin} */ + const plugin = { + name: 'remove-prerender-export', + transform(code, id) { + if (!EXTENSIONS.some((ext) => id.endsWith(ext))) return; + return code.replace(/export\s+const\s+prerender\s+=\s+true;/g, ''); + }, + }; + return { + name: 'remove-prerender-export-injector', + configResolved(resolved) { + resolved.plugins.unshift(plugin); + }, + }; +} From d2a608560442b00e90c7d41fd0629075b5d0e29b Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Mon, 22 May 2023 12:02:21 +0000 Subject: [PATCH 18/18] chore: update lock file --- pnpm-lock.yaml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6c975070a14f..20ca7bcb57b4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3278,12 +3278,6 @@ importers: specifier: workspace:* version: link:../../.. - packages/astro/test/fixtures/ssr-hybrid-get-static-paths: - dependencies: - astro: - specifier: workspace:* - version: link:../../.. - packages/astro/test/fixtures/ssr-manifest: dependencies: astro: