diff --git a/.changeset/ten-hounds-fry.md b/.changeset/ten-hounds-fry.md new file mode 100644 index 0000000000..9db3680d2d --- /dev/null +++ b/.changeset/ten-hounds-fry.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Expose route dist URL on SSG diff --git a/.gitignore b/.gitignore index fa9abb6d0e..8967dc4b0c 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,6 @@ package-lock.json !packages/astro/vendor/vite/dist packages/integrations/**/.netlify/ + +# exclude IntelliJ/WebStorm stuff +.idea diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index ada2427ad9..31f9e98f3c 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -963,6 +963,8 @@ export interface RouteData { generate: (data?: any) => string; params: string[]; pathname?: string; + // expose the real path name on SSG + distURL?: URL; pattern: RegExp; segments: RoutePart[][]; type: RouteType; diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index d0dec38be0..5a9965eee3 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -250,6 +250,7 @@ async function generatePath( const outFolder = getOutFolder(astroConfig, pathname, pageData.route.type); const outFile = getOutFile(astroConfig, outFolder, pathname, pageData.route.type); + pageData.route.distURL = outFile; await fs.promises.mkdir(outFolder, { recursive: true }); await fs.promises.writeFile(outFile, body, 'utf-8'); } diff --git a/packages/astro/test/static-build-page-dist-url.test.js b/packages/astro/test/static-build-page-dist-url.test.js new file mode 100644 index 0000000000..0d5bd09aa6 --- /dev/null +++ b/packages/astro/test/static-build-page-dist-url.test.js @@ -0,0 +1,34 @@ +import { expect } from 'chai'; +import { loadFixture } from './test-utils.js'; + +describe('Static build: pages routes have distURL', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + /** @type {RouteData[]} */ + let checkRoutes + before(async () => { + const fixture = await loadFixture({ + root: './fixtures/astro pages/', + integrations: [{ + name: '@astrojs/distURL', + hooks: { + 'astro:build:done': ({ routes }) => { + checkRoutes = routes.filter(p => p.type === 'page') + }, + }, + }] + }); + await fixture.build(); + }) + it('Pages routes have distURL', async () => { + expect(checkRoutes).to.have.lengthOf.above(0, 'Pages not found: build end hook not being called') + checkRoutes.forEach(p => expect(p).to.have.property( + 'distURL' + ).that.is.a( + 'URL', `${p.pathname} doesn't include distURL` + )); + }); +}); + + +