Account for pathnames being part of the site config (#856)

* Account for pathnames being part of the site config

* Adds a changeset

* Don't toString the site in the test
This commit is contained in:
Matthew Phillips 2021-07-26 14:42:24 -04:00 committed by GitHub
parent 490d6e7452
commit 4726e34408
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 21 additions and 12 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes cases where buildOptions.site is not respected

View file

@ -11,7 +11,11 @@ export function canonicalURL(url: string, base?: string): URL {
pathname = pathname.replace(/\/1\/?$/, ''); // neither is a trailing /1/ (impl. detail of collections)
if (!path.extname(pathname)) pathname = pathname.replace(/(\/+)?$/, '/'); // add trailing slash if theres no extension
pathname = pathname.replace(/\/+/g, '/'); // remove duplicate slashes (URL() wont)
return new URL(pathname, base);
if(base) {
return new URL('.' + pathname, base);
} else {
return new URL(pathname, base);
}
}
/** Resolve final output URL */

View file

@ -117,7 +117,7 @@ ${result.imports.join('\n')}
${/* Global Astro Namespace (shadowed & extended by the scoped namespace inside of __render()) */ ''}
const __TopLevelAstro = {
site: new URL('/', ${JSON.stringify(site)}),
site: new URL(${JSON.stringify(site)}),
fetchContent: (globResult) => fetchContent(globResult, import.meta.url),
};
const Astro = __TopLevelAstro;

View file

@ -68,8 +68,8 @@ async function load(config: RuntimeConfig, rawPathname: string | undefined): Pro
const { logging, snowpackRuntime, snowpack, configManager } = config;
const { buildOptions, devOptions } = config.astroConfig;
let origin = buildOptions.site ? new URL(buildOptions.site).origin : `http://${devOptions.hostname}:${devOptions.port}`;
const fullurl = new URL(rawPathname || '/', origin);
const site = new URL(buildOptions.site || `http://${devOptions.hostname}:${devOptions.port}`);
const fullurl = new URL(rawPathname || '/', site.origin);
const reqPath = decodeURI(fullurl.pathname);
info(logging, 'access', reqPath);
@ -214,7 +214,7 @@ async function load(config: RuntimeConfig, rawPathname: string | undefined): Pro
request: {
// params should go here when implemented
url: requestURL,
canonicalURL: canonicalURL(requestURL.pathname, requestURL.origin),
canonicalURL: canonicalURL(requestURL.pathname, site.toString()),
},
children: [],
props: pageProps,

View file

@ -18,10 +18,10 @@ Global('Astro.request.url', async (context) => {
Global('Astro.request.canonicalURL', async (context) => {
// given a URL, expect the following canonical URL
const canonicalURLs = {
'/': 'https://mysite.dev/',
'/post/post': 'https://mysite.dev/post/post/',
'/posts': 'https://mysite.dev/posts/',
'/posts/2': 'https://mysite.dev/posts/2/',
'/': 'https://mysite.dev/blog/',
'/post/post': 'https://mysite.dev/blog/post/post/',
'/posts': 'https://mysite.dev/blog/posts/',
'/posts/2': 'https://mysite.dev/blog/posts/2/',
};
for (const [url, canonicalURL] of Object.entries(canonicalURLs)) {
@ -36,7 +36,7 @@ Global('Astro.site', async (context) => {
assert.ok(!result.error, `build error: ${result.error}`);
const $ = doc(result.contents);
assert.equal($('#site').attr('href'), 'https://mysite.dev');
assert.equal($('#site').attr('href'), 'https://mysite.dev/blog/');
});
Global.run();

View file

@ -1,6 +1,6 @@
export default {
buildOptions: {
site: 'https://mysite.dev',
site: 'https://mysite.dev/blog/',
sitemap: false,
},
};

View file

@ -5,6 +5,6 @@
</head>
<body>
<div id="pathname">{Astro.request.url.pathname}</div>
<a id="site" href={Astro.site.origin}>Home</a>
<a id="site" href={Astro.site}>Home</a>
</body>
</html>