From 3eb96a7ab768a807d2b665dfa692ca9d6ae18d20 Mon Sep 17 00:00:00 2001 From: Tony Sullivan Date: Tue, 7 Jun 2022 21:20:20 +0000 Subject: [PATCH] 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 --- .changeset/brown-peas-destroy.md | 5 ++ .../astro/src/vite-plugin-astro/compile.ts | 2 +- .../astro/src/vite-plugin-markdown/index.ts | 2 +- packages/astro/test/astro-global.test.js | 73 +++++++++++++++++++ .../fixtures/astro-global/astro.config.mjs | 4 - 5 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 .changeset/brown-peas-destroy.md delete mode 100644 packages/astro/test/fixtures/astro-global/astro.config.mjs diff --git a/.changeset/brown-peas-destroy.md b/.changeset/brown-peas-destroy.md new file mode 100644 index 000000000..f4bbb7586 --- /dev/null +++ b/.changeset/brown-peas-destroy.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix: Astro.site should default to localhost when not provided in a project's config diff --git a/packages/astro/src/vite-plugin-astro/compile.ts b/packages/astro/src/vite-plugin-astro/compile.ts index 09dcbbe57..356d602de 100644 --- a/packages/astro/src/vite-plugin-astro/compile.ts +++ b/packages/astro/src/vite-plugin-astro/compile.ts @@ -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( diff --git a/packages/astro/src/vite-plugin-markdown/index.ts b/packages/astro/src/vite-plugin-markdown/index.ts index 6712e0cbb..f64250bdd 100644 --- a/packages/astro/src/vite-plugin-markdown/index.ts +++ b/packages/astro/src/vite-plugin-markdown/index.ts @@ -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 diff --git a/packages/astro/test/astro-global.test.js b/packages/astro/test/astro-global.test.js index 799f89071..5a26270a0 100644 --- a/packages/astro/test/astro-global.test.js +++ b/packages/astro/test/astro-global.test.js @@ -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+\//); + }); + }); +}); diff --git a/packages/astro/test/fixtures/astro-global/astro.config.mjs b/packages/astro/test/fixtures/astro-global/astro.config.mjs deleted file mode 100644 index d87985387..000000000 --- a/packages/astro/test/fixtures/astro-global/astro.config.mjs +++ /dev/null @@ -1,4 +0,0 @@ -export default { - site: 'https://mysite.dev/', - base: '/blog' -}