Skip to content

Commit

Permalink
Merge branch 'main' into cf-fix-sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
caioferrarezi authored Jun 7, 2022
2 parents f3b6d26 + 47d1587 commit 49d1f6d
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-peas-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix: Astro.site should default to localhost when not provided in a project's config
5 changes: 5 additions & 0 deletions .changeset/tough-papayas-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix: show "unable to find network to expose" with local network log when using --host without suitable networks
50 changes: 26 additions & 24 deletions packages/astro/src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,42 +76,44 @@ 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 = [
`${emoji('🚀 ', '')}${bgGreen(black(` astro `))} ${green(`v${version}`)} ${dim(
`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() {
Expand Down
4 changes: 3 additions & 1 deletion packages/astro/src/vite-plugin-astro/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() : undefined,
site: config.site
? new URL(config.base, config.site).toString()
: `http://localhost:${config.server.port}/`,
sourcefile: filename,
sourcemap: 'both',
internalURL: `/@fs${prependForwardSlash(
Expand Down
4 changes: 3 additions & 1 deletion packages/astro/src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() : undefined,
site: config.site
? new URL(config.base, config.site).toString()
: `http://localhost:${config.server.port}/`,
sourcefile: id,
sourcemap: 'inline',
// TODO: baseline flag
Expand Down
73 changes: 73 additions & 0 deletions packages/astro/test/astro-global.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ describe('Astro Global', () => {
before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-global/',
site: 'https://mysite.dev/',
base: '/blog',
});
});

Expand Down Expand Up @@ -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+\//);
});
});
});
4 changes: 0 additions & 4 deletions packages/astro/test/fixtures/astro-global/astro.config.mjs

This file was deleted.

0 comments on commit 49d1f6d

Please sign in to comment.