mirror of
https://github.com/withastro/astro.git
synced 2025-02-17 22:44:24 -05:00
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:
parent
490d6e7452
commit
4726e34408
7 changed files with 21 additions and 12 deletions
5
.changeset/shy-seahorses-clean.md
Normal file
5
.changeset/shy-seahorses-clean.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixes cases where buildOptions.site is not respected
|
|
@ -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)
|
pathname = pathname.replace(/\/1\/?$/, ''); // neither is a trailing /1/ (impl. detail of collections)
|
||||||
if (!path.extname(pathname)) pathname = pathname.replace(/(\/+)?$/, '/'); // add trailing slash if there’s no extension
|
if (!path.extname(pathname)) pathname = pathname.replace(/(\/+)?$/, '/'); // add trailing slash if there’s no extension
|
||||||
pathname = pathname.replace(/\/+/g, '/'); // remove duplicate slashes (URL() won’t)
|
pathname = pathname.replace(/\/+/g, '/'); // remove duplicate slashes (URL() won’t)
|
||||||
return new URL(pathname, base);
|
if(base) {
|
||||||
|
return new URL('.' + pathname, base);
|
||||||
|
} else {
|
||||||
|
return new URL(pathname, base);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Resolve final output URL */
|
/** Resolve final output URL */
|
||||||
|
|
|
@ -117,7 +117,7 @@ ${result.imports.join('\n')}
|
||||||
|
|
||||||
${/* Global Astro Namespace (shadowed & extended by the scoped namespace inside of __render()) */ ''}
|
${/* Global Astro Namespace (shadowed & extended by the scoped namespace inside of __render()) */ ''}
|
||||||
const __TopLevelAstro = {
|
const __TopLevelAstro = {
|
||||||
site: new URL('/', ${JSON.stringify(site)}),
|
site: new URL(${JSON.stringify(site)}),
|
||||||
fetchContent: (globResult) => fetchContent(globResult, import.meta.url),
|
fetchContent: (globResult) => fetchContent(globResult, import.meta.url),
|
||||||
};
|
};
|
||||||
const Astro = __TopLevelAstro;
|
const Astro = __TopLevelAstro;
|
||||||
|
|
|
@ -68,8 +68,8 @@ async function load(config: RuntimeConfig, rawPathname: string | undefined): Pro
|
||||||
const { logging, snowpackRuntime, snowpack, configManager } = config;
|
const { logging, snowpackRuntime, snowpack, configManager } = config;
|
||||||
const { buildOptions, devOptions } = config.astroConfig;
|
const { buildOptions, devOptions } = config.astroConfig;
|
||||||
|
|
||||||
let origin = buildOptions.site ? new URL(buildOptions.site).origin : `http://${devOptions.hostname}:${devOptions.port}`;
|
const site = new URL(buildOptions.site || `http://${devOptions.hostname}:${devOptions.port}`);
|
||||||
const fullurl = new URL(rawPathname || '/', origin);
|
const fullurl = new URL(rawPathname || '/', site.origin);
|
||||||
|
|
||||||
const reqPath = decodeURI(fullurl.pathname);
|
const reqPath = decodeURI(fullurl.pathname);
|
||||||
info(logging, 'access', reqPath);
|
info(logging, 'access', reqPath);
|
||||||
|
@ -214,7 +214,7 @@ async function load(config: RuntimeConfig, rawPathname: string | undefined): Pro
|
||||||
request: {
|
request: {
|
||||||
// params should go here when implemented
|
// params should go here when implemented
|
||||||
url: requestURL,
|
url: requestURL,
|
||||||
canonicalURL: canonicalURL(requestURL.pathname, requestURL.origin),
|
canonicalURL: canonicalURL(requestURL.pathname, site.toString()),
|
||||||
},
|
},
|
||||||
children: [],
|
children: [],
|
||||||
props: pageProps,
|
props: pageProps,
|
||||||
|
|
|
@ -18,10 +18,10 @@ Global('Astro.request.url', async (context) => {
|
||||||
Global('Astro.request.canonicalURL', async (context) => {
|
Global('Astro.request.canonicalURL', async (context) => {
|
||||||
// given a URL, expect the following canonical URL
|
// given a URL, expect the following canonical URL
|
||||||
const canonicalURLs = {
|
const canonicalURLs = {
|
||||||
'/': 'https://mysite.dev/',
|
'/': 'https://mysite.dev/blog/',
|
||||||
'/post/post': 'https://mysite.dev/post/post/',
|
'/post/post': 'https://mysite.dev/blog/post/post/',
|
||||||
'/posts': 'https://mysite.dev/posts/',
|
'/posts': 'https://mysite.dev/blog/posts/',
|
||||||
'/posts/2': 'https://mysite.dev/posts/2/',
|
'/posts/2': 'https://mysite.dev/blog/posts/2/',
|
||||||
};
|
};
|
||||||
|
|
||||||
for (const [url, canonicalURL] of Object.entries(canonicalURLs)) {
|
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}`);
|
assert.ok(!result.error, `build error: ${result.error}`);
|
||||||
|
|
||||||
const $ = doc(result.contents);
|
const $ = doc(result.contents);
|
||||||
assert.equal($('#site').attr('href'), 'https://mysite.dev');
|
assert.equal($('#site').attr('href'), 'https://mysite.dev/blog/');
|
||||||
});
|
});
|
||||||
|
|
||||||
Global.run();
|
Global.run();
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
export default {
|
export default {
|
||||||
buildOptions: {
|
buildOptions: {
|
||||||
site: 'https://mysite.dev',
|
site: 'https://mysite.dev/blog/',
|
||||||
sitemap: false,
|
sitemap: false,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,6 +5,6 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="pathname">{Astro.request.url.pathname}</div>
|
<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>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Add table
Reference in a new issue