[MINOR] standardize trailing subpath, fix fetchContent url issue (#2471)

* standardize trailing subpath, and fix fetchcontent issue

* debug windows ci

* improve ci test

* fix windows test issue?

* fix only usage

* end debugging
This commit is contained in:
Fred K. Schott 2022-01-28 17:29:53 -08:00 committed by GitHub
parent 6bd165f84c
commit c9bb1147cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 9 deletions

View file

@ -0,0 +1,7 @@
---
'astro': patch
---
Respect subpath URL paths in the fetchContent url property.
This fixes an issue where fetchContent() URL property did not include the buildOptions.site path in it.

View file

@ -0,0 +1,7 @@
---
'astro': minor
---
Standardize trailing subpath behavior in config.
Most users are not aware of the subtle differences between `/foo` and `/foo/`. Internally, we have to handle both which means that we are constantly worrying about the format of the URL, needing to add/remove trailing slashes when we go to work with this property, etc. This change transforms all `site` values to use a trailing slash internally, which should help reduce bugs for both users and maintainers.

View file

@ -53,7 +53,10 @@ export const AstroConfigSchema = z.object({
.default({}), .default({}),
buildOptions: z buildOptions: z
.object({ .object({
site: z.string().optional(), site: z
.string()
.optional()
.transform((val) => (val ? addTrailingSlash(val) : val)),
sitemap: z.boolean().optional().default(true), sitemap: z.boolean().optional().default(true),
pageUrlFormat: z pageUrlFormat: z
.union([z.literal('file'), z.literal('directory')]) .union([z.literal('file'), z.literal('directory')])

View file

@ -262,7 +262,7 @@ If you're still stuck, please open an issue on GitHub or join us at https://astr
} }
/** Create the Astro.fetchContent() runtime function. */ /** Create the Astro.fetchContent() runtime function. */
function createFetchContentFn(url: URL) { function createFetchContentFn(url: URL, site: URL) {
const fetchContent = (importMetaGlobResult: Record<string, any>) => { const fetchContent = (importMetaGlobResult: Record<string, any>) => {
let allEntries = [...Object.entries(importMetaGlobResult)]; let allEntries = [...Object.entries(importMetaGlobResult)];
if (allEntries.length === 0) { if (allEntries.length === 0) {
@ -280,7 +280,7 @@ function createFetchContentFn(url: URL) {
Content: mod.default, Content: mod.default,
content: mod.metadata, content: mod.metadata,
file: new URL(spec, url), file: new URL(spec, url),
url: urlSpec.includes('/pages/') ? urlSpec.replace(/^.*\/pages\//, '/').replace(/(\/index)?\.md$/, '') : undefined, url: urlSpec.includes('/pages/') ? urlSpec.replace(/^.*\/pages\//, site.pathname).replace(/(\/index)?\.md$/, '') : undefined,
}; };
}) })
.filter(Boolean); .filter(Boolean);
@ -293,12 +293,13 @@ function createFetchContentFn(url: URL) {
// This is used to create the top-level Astro global; the one that you can use // This is used to create the top-level Astro global; the one that you can use
// Inside of getStaticPaths. // Inside of getStaticPaths.
export function createAstro(filePathname: string, site: string, projectRootStr: string): AstroGlobalPartial { export function createAstro(filePathname: string, _site: string, projectRootStr: string): AstroGlobalPartial {
const site = new URL(_site);
const url = new URL(filePathname, site); const url = new URL(filePathname, site);
const projectRoot = new URL(projectRootStr); const projectRoot = new URL(projectRootStr);
const fetchContent = createFetchContentFn(url); const fetchContent = createFetchContentFn(url, site);
return { return {
site: new URL(site), site,
fetchContent, fetchContent,
// INVESTIGATE is there a use-case for multi args? // INVESTIGATE is there a use-case for multi args?
resolve(...segments: string[]) { resolve(...segments: string[]) {

View file

@ -54,4 +54,10 @@ describe('Astro.*', () => {
expect($('img').attr('src')).to.include('assets/penguin.ccd44411.png'); // Main src/images expect($('img').attr('src')).to.include('assets/penguin.ccd44411.png'); // Main src/images
expect($('#inner-child img').attr('src')).to.include('assets/penguin.b9ab122a.png'); expect($('#inner-child img').attr('src')).to.include('assets/penguin.b9ab122a.png');
}); });
it('Astro.fetchContent() returns the correct "url" property, including buildOptions.site subpath', async () => {
const html = await fixture.readFile('/posts/1/index.html');
const $ = cheerio.load(html);
expect($('.post-url').attr('href')).to.equal('/blog/post/post-2');
});
}); });

View file

@ -151,9 +151,9 @@ describe('Development Routing', () => {
expect(response.status).to.equal(200); expect(response.status).to.equal(200);
}); });
it('200 when loading subpath root without trailing slash', async () => { it('404 when loading subpath root without trailing slash', async () => {
const response = await fixture.fetch('/blog'); const response = await fixture.fetch('/blog');
expect(response.status).to.equal(200); expect(response.status).to.equal(404);
}); });
it('200 when loading another page with subpath used', async () => { it('200 when loading another page with subpath used', async () => {

View file

@ -16,7 +16,7 @@ const { params, canonicalURL} = Astro.request;
{page.data.map((data) => ( {page.data.map((data) => (
<div> <div>
<h1>{data.title}</h1> <h1>{data.title}</h1>
<a href={data.url}>Read</a> <a class="post-url" href={data.url}>Read</a>
</div> </div>
))} ))}
</body> </body>