feat: expose route dist URL on SSG (#3438)

* feat: expose route dist URL on SSG

* chore: add changeset

* chore: add test for `distURL`

* cleanup: remove console.log from test
This commit is contained in:
Joaquín Sánchez 2022-05-26 16:19:19 +02:00 committed by GitHub
parent c4658ba857
commit 79b9ebc83a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Expose route dist URL on SSG

3
.gitignore vendored
View file

@ -18,3 +18,6 @@ package-lock.json
!packages/astro/vendor/vite/dist
packages/integrations/**/.netlify/
# exclude IntelliJ/WebStorm stuff
.idea

View file

@ -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;

View file

@ -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');
}

View file

@ -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`
));
});
});