summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Fred K. Schott <fkschott@gmail.com> 2022-01-28 17:29:53 -0800
committerGravatar GitHub <noreply@github.com> 2022-01-28 20:29:53 -0500
commitc9bb1147cbfae20e3ecdf29ef2866a183b3b18e3 (patch)
treed79bdb4c50fa0c6504be4cdcbe8369e86b3910c2
parent6bd165f84cd3a1550b29fec539af814360c87f54 (diff)
downloadastro-c9bb1147cbfae20e3ecdf29ef2866a183b3b18e3.tar.gz
astro-c9bb1147cbfae20e3ecdf29ef2866a183b3b18e3.tar.zst
astro-c9bb1147cbfae20e3ecdf29ef2866a183b3b18e3.zip
[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
-rw-r--r--.changeset/old-parents-obey.md7
-rw-r--r--.changeset/shaggy-shoes-leave.md7
-rw-r--r--packages/astro/src/core/config.ts5
-rw-r--r--packages/astro/src/runtime/server/index.ts11
-rw-r--r--packages/astro/test/astro-global.test.js6
-rw-r--r--packages/astro/test/dev-routing.test.js4
-rw-r--r--packages/astro/test/fixtures/astro-global/src/pages/posts/[page].astro2
7 files changed, 33 insertions, 9 deletions
diff --git a/.changeset/old-parents-obey.md b/.changeset/old-parents-obey.md
new file mode 100644
index 000000000..0e54404da
--- /dev/null
+++ b/.changeset/old-parents-obey.md
@@ -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.
diff --git a/.changeset/shaggy-shoes-leave.md b/.changeset/shaggy-shoes-leave.md
new file mode 100644
index 000000000..a92abed9f
--- /dev/null
+++ b/.changeset/shaggy-shoes-leave.md
@@ -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. \ No newline at end of file
diff --git a/packages/astro/src/core/config.ts b/packages/astro/src/core/config.ts
index 4c0b75449..186677802 100644
--- a/packages/astro/src/core/config.ts
+++ b/packages/astro/src/core/config.ts
@@ -53,7 +53,10 @@ export const AstroConfigSchema = z.object({
.default({}),
buildOptions: z
.object({
- site: z.string().optional(),
+ site: z
+ .string()
+ .optional()
+ .transform((val) => (val ? addTrailingSlash(val) : val)),
sitemap: z.boolean().optional().default(true),
pageUrlFormat: z
.union([z.literal('file'), z.literal('directory')])
diff --git a/packages/astro/src/runtime/server/index.ts b/packages/astro/src/runtime/server/index.ts
index fd401fbc8..894f9867d 100644
--- a/packages/astro/src/runtime/server/index.ts
+++ b/packages/astro/src/runtime/server/index.ts
@@ -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. */
-function createFetchContentFn(url: URL) {
+function createFetchContentFn(url: URL, site: URL) {
const fetchContent = (importMetaGlobResult: Record<string, any>) => {
let allEntries = [...Object.entries(importMetaGlobResult)];
if (allEntries.length === 0) {
@@ -280,7 +280,7 @@ function createFetchContentFn(url: URL) {
Content: mod.default,
content: mod.metadata,
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);
@@ -293,12 +293,13 @@ function createFetchContentFn(url: URL) {
// This is used to create the top-level Astro global; the one that you can use
// 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 projectRoot = new URL(projectRootStr);
- const fetchContent = createFetchContentFn(url);
+ const fetchContent = createFetchContentFn(url, site);
return {
- site: new URL(site),
+ site,
fetchContent,
// INVESTIGATE is there a use-case for multi args?
resolve(...segments: string[]) {
diff --git a/packages/astro/test/astro-global.test.js b/packages/astro/test/astro-global.test.js
index 168d66bf9..e479eb4e6 100644
--- a/packages/astro/test/astro-global.test.js
+++ b/packages/astro/test/astro-global.test.js
@@ -54,4 +54,10 @@ describe('Astro.*', () => {
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');
});
+
+ 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');
+ });
});
diff --git a/packages/astro/test/dev-routing.test.js b/packages/astro/test/dev-routing.test.js
index 1f5647935..198445818 100644
--- a/packages/astro/test/dev-routing.test.js
+++ b/packages/astro/test/dev-routing.test.js
@@ -151,9 +151,9 @@ describe('Development Routing', () => {
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');
- expect(response.status).to.equal(200);
+ expect(response.status).to.equal(404);
});
it('200 when loading another page with subpath used', async () => {
diff --git a/packages/astro/test/fixtures/astro-global/src/pages/posts/[page].astro b/packages/astro/test/fixtures/astro-global/src/pages/posts/[page].astro
index e8df864ee..e684161e6 100644
--- a/packages/astro/test/fixtures/astro-global/src/pages/posts/[page].astro
+++ b/packages/astro/test/fixtures/astro-global/src/pages/posts/[page].astro
@@ -16,7 +16,7 @@ const { params, canonicalURL} = Astro.request;
{page.data.map((data) => (
<div>
<h1>{data.title}</h1>
- <a href={data.url}>Read</a>
+ <a class="post-url" href={data.url}>Read</a>
</div>
))}
</body>