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:
parent
c5db640dd2
commit
3eb96a7ab7
5 changed files with 80 additions and 6 deletions
5
.changeset/brown-peas-destroy.md
Normal file
5
.changeset/brown-peas-destroy.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix: Astro.site should default to localhost when not provided in a project's config
|
|
@ -63,7 +63,7 @@ async function compile({
|
||||||
// For Windows compat, prepend the module ID with `/@fs`
|
// For Windows compat, prepend the module ID with `/@fs`
|
||||||
pathname: `/@fs${prependForwardSlash(moduleId)}`,
|
pathname: `/@fs${prependForwardSlash(moduleId)}`,
|
||||||
projectRoot: config.root.toString(),
|
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,
|
sourcefile: filename,
|
||||||
sourcemap: 'both',
|
sourcemap: 'both',
|
||||||
internalURL: `/@fs${prependForwardSlash(
|
internalURL: `/@fs${prependForwardSlash(
|
||||||
|
|
|
@ -165,7 +165,7 @@ ${setup}`.trim();
|
||||||
let { code: tsResult } = await transform(astroResult, {
|
let { code: tsResult } = await transform(astroResult, {
|
||||||
pathname: '/@fs' + prependForwardSlash(fileUrl.pathname),
|
pathname: '/@fs' + prependForwardSlash(fileUrl.pathname),
|
||||||
projectRoot: config.root.toString(),
|
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,
|
sourcefile: id,
|
||||||
sourcemap: 'inline',
|
sourcemap: 'inline',
|
||||||
// TODO: baseline flag
|
// TODO: baseline flag
|
||||||
|
|
|
@ -8,6 +8,8 @@ describe('Astro Global', () => {
|
||||||
before(async () => {
|
before(async () => {
|
||||||
fixture = await loadFixture({
|
fixture = await loadFixture({
|
||||||
root: './fixtures/astro-global/',
|
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+\//);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
export default {
|
|
||||||
site: 'https://mysite.dev/',
|
|
||||||
base: '/blog'
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue