From a83d5817141081ac28f84a436c177af63decd831 Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Tue, 7 Jun 2022 17:12:35 -0400 Subject: [PATCH 1/4] Fix: `--host` flag logs when no network IPs are found (#3547) * feat: add fallback log if no network interfaces found * fix: extra newline on missing network log * chore: changeset --- .changeset/tough-papayas-jam.md | 5 +++ packages/astro/src/core/messages.ts | 47 ++++++++++++++--------------- 2 files changed, 28 insertions(+), 24 deletions(-) create mode 100644 .changeset/tough-papayas-jam.md diff --git a/.changeset/tough-papayas-jam.md b/.changeset/tough-papayas-jam.md new file mode 100644 index 000000000000..af72144df406 --- /dev/null +++ b/.changeset/tough-papayas-jam.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix: show "unable to find network to expose" with local network log when using --host without suitable networks diff --git a/packages/astro/src/core/messages.ts b/packages/astro/src/core/messages.ts index d6a263ec4d4c..82815e7d7de0 100644 --- a/packages/astro/src/core/messages.ts +++ b/packages/astro/src/core/messages.ts @@ -76,31 +76,29 @@ export function devStart({ const networkLogging = getNetworkLogging(config.server.host); const toDisplayUrl = (hostname: string) => `${https ? 'https' : 'http'}://${hostname}:${port}${rootPath}`; - let addresses = []; - - if (networkLogging === 'none') { - addresses = [`${localPrefix}${bold(cyan(toDisplayUrl(localAddress)))}`]; - } else if (networkLogging === 'host-to-expose') { - addresses = [ - `${localPrefix}${bold(cyan(toDisplayUrl(localAddress)))}`, - `${networkPrefix}${dim('use --host to expose')}`, - ]; - } else { - addresses = Object.values(os.networkInterfaces()) + + let local = `${localPrefix}${bold(cyan(toDisplayUrl(localAddress)))}`; + let network = null; + + if (networkLogging === 'host-to-expose') { + network = `${networkPrefix}${dim('use --host to expose')}`; + } else if (networkLogging === 'visible') { + const ipv4Networks = Object.values(os.networkInterfaces()) .flatMap((networkInterface) => networkInterface ?? []) .filter( (networkInterface) => networkInterface?.address && networkInterface?.family === 'IPv4' - ) - .map(({ address }) => { - if (address.includes('127.0.0.1')) { - const displayAddress = address.replace('127.0.0.1', localAddress); - return `${localPrefix}${bold(cyan(toDisplayUrl(displayAddress)))}`; - } else { - return `${networkPrefix}${bold(cyan(toDisplayUrl(address)))}`; - } - }) - // ensure Local logs come before Network - .sort((msg) => (msg.startsWith(localPrefix) ? -1 : 1)); + ); + for (let { address } of ipv4Networks) { + if (address.includes('127.0.0.1')) { + const displayAddress = address.replace('127.0.0.1', localAddress); + local = `${localPrefix}${bold(cyan(toDisplayUrl(displayAddress)))}`; + } else { + network = `${networkPrefix}${bold(cyan(toDisplayUrl(address)))}`; + } + } + if (!network) { + network = `${networkPrefix}${dim('unable to find network to expose')}`; + } } const messages = [ @@ -108,10 +106,11 @@ export function devStart({ `started in ${Math.round(startupTime)}ms` )}`, '', - ...addresses, + local, + network, '', ]; - return messages.map((msg) => ` ${msg}`).join('\n'); + return messages.filter((msg) => typeof msg === 'string').map((msg) => ` ${msg}`).join('\n'); } export function telemetryNotice() { From c5db640dd25c59c0b83a02bed9ffca82b20305fb Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 7 Jun 2022 21:14:44 +0000 Subject: [PATCH 2/4] [ci] format --- packages/astro/src/core/messages.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/core/messages.ts b/packages/astro/src/core/messages.ts index 82815e7d7de0..05b363c4bb09 100644 --- a/packages/astro/src/core/messages.ts +++ b/packages/astro/src/core/messages.ts @@ -110,7 +110,10 @@ export function devStart({ network, '', ]; - return messages.filter((msg) => typeof msg === 'string').map((msg) => ` ${msg}`).join('\n'); + return messages + .filter((msg) => typeof msg === 'string') + .map((msg) => ` ${msg}`) + .join('\n'); } export function telemetryNotice() { From 3eb96a7ab768a807d2b665dfa692ca9d6ae18d20 Mon Sep 17 00:00:00 2001 From: Tony Sullivan Date: Tue, 7 Jun 2022 21:20:20 +0000 Subject: [PATCH 3/4] 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 000000000000..f4bbb758663a --- /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 09dcbbe57926..356d602de1bc 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 6712e0cbbb64..f64250bdd539 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 799f890717ab..5a26270a0962 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 d87985387c07..000000000000 --- a/packages/astro/test/fixtures/astro-global/astro.config.mjs +++ /dev/null @@ -1,4 +0,0 @@ -export default { - site: 'https://mysite.dev/', - base: '/blog' -} From 47d15870ff986610edaa08906d891cac10922fec Mon Sep 17 00:00:00 2001 From: tony-sull Date: Tue, 7 Jun 2022 21:22:13 +0000 Subject: [PATCH 4/4] [ci] format --- packages/astro/src/vite-plugin-astro/compile.ts | 4 +++- packages/astro/src/vite-plugin-markdown/index.ts | 4 +++- packages/astro/test/astro-global.test.js | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/astro/src/vite-plugin-astro/compile.ts b/packages/astro/src/vite-plugin-astro/compile.ts index 356d602de1bc..5a6e199d7197 100644 --- a/packages/astro/src/vite-plugin-astro/compile.ts +++ b/packages/astro/src/vite-plugin-astro/compile.ts @@ -63,7 +63,9 @@ 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() : `http://localhost:${config.server.port}/`, + 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 f64250bdd539..1a0b9f228929 100644 --- a/packages/astro/src/vite-plugin-markdown/index.ts +++ b/packages/astro/src/vite-plugin-markdown/index.ts @@ -165,7 +165,9 @@ ${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() : `http://localhost:${config.server.port}/`, + 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 5a26270a0962..a68ae039fb27 100644 --- a/packages/astro/test/astro-global.test.js +++ b/packages/astro/test/astro-global.test.js @@ -9,7 +9,7 @@ describe('Astro Global', () => { fixture = await loadFixture({ root: './fixtures/astro-global/', site: 'https://mysite.dev/', - base: '/blog' + base: '/blog', }); }); @@ -93,7 +93,7 @@ describe('Astro Global Defaults', () => { before(async () => { fixture = await loadFixture({ - root: './fixtures/astro-global/' + root: './fixtures/astro-global/', }); });