Fix: Astro.site should default to localhost if not provided in config (#3552)

* Astro.site should be defaulted to localhost

* test: verify Astro.site default value

* chore: add changeset

* test: matching a URL regex to ignore specific port numbers
This commit is contained in:
Tony Sullivan 2022-06-07 21:20:20 +00:00 committed by GitHub
parent c5db640dd2
commit 3eb96a7ab7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 6 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix: Astro.site should default to localhost when not provided in a project's config

View file

@ -63,7 +63,7 @@ async function compile({
// For Windows compat, prepend the module ID with `/@fs`
pathname: `/@fs${prependForwardSlash(moduleId)}`,
projectRoot: config.root.toString(),
site: config.site ? new URL(config.base, config.site).toString() : undefined,
site: config.site ? new URL(config.base, config.site).toString() : `http://localhost:${config.server.port}/`,
sourcefile: filename,
sourcemap: 'both',
internalURL: `/@fs${prependForwardSlash(

View file

@ -165,7 +165,7 @@ ${setup}`.trim();
let { code: tsResult } = await transform(astroResult, {
pathname: '/@fs' + prependForwardSlash(fileUrl.pathname),
projectRoot: config.root.toString(),
site: config.site ? new URL(config.base, config.site).toString() : undefined,
site: config.site ? new URL(config.base, config.site).toString() : `http://localhost:${config.server.port}/`,
sourcefile: id,
sourcemap: 'inline',
// TODO: baseline flag

View file

@ -8,6 +8,8 @@ describe('Astro Global', () => {
before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-global/',
site: 'https://mysite.dev/',
base: '/blog'
});
});
@ -85,3 +87,74 @@ describe('Astro Global', () => {
});
});
});
describe('Astro Global Defaults', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-global/'
});
});
describe('dev', () => {
let devServer;
let $;
before(async () => {
devServer = await fixture.startDevServer();
const html = await fixture.fetch('/blog/?foo=42').then((res) => res.text());
$ = cheerio.load(html);
});
after(async () => {
await devServer.stop();
});
it('Astro.request.url', async () => {
expect($('#pathname').text()).to.equal('');
expect($('#searchparams').text()).to.equal('');
expect($('#child-pathname').text()).to.equal('');
expect($('#nested-child-pathname').text()).to.equal('');
});
});
describe('build', () => {
before(async () => {
await fixture.build();
});
it('Astro.request.url', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
expect($('#pathname').text()).to.equal('/');
expect($('#searchparams').text()).to.equal('{}');
expect($('#child-pathname').text()).to.equal('/');
expect($('#nested-child-pathname').text()).to.equal('/');
});
it('Astro.canonicalURL', async () => {
// given a URL, expect the following canonical URL
const canonicalURLs = {
'/index.html': /http:\/\/localhost:\d+\//,
'/post/post/index.html': /http:\/\/localhost:\d+\/post\/post\//,
'/posts/1/index.html': /http:\/\/localhost:\d+\/posts\//,
'/posts/2/index.html': /http:\/\/localhost:\d+\/posts\/2\//,
};
for (const [url, canonicalURL] of Object.entries(canonicalURLs)) {
const html = await fixture.readFile(url);
const $ = cheerio.load(html);
expect($('link[rel="canonical"]').attr('href')).to.match(canonicalURL);
}
});
it('Astro.site', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
expect($('#site').attr('href')).to.match(/http:\/\/localhost:\d+\//);
});
});
});

View file

@ -1,4 +0,0 @@
export default {
site: 'https://mysite.dev/',
base: '/blog'
}