Skip to content

Commit 49d1f6d

Browse files
Merge branch 'main' into cf-fix-sitemap
2 parents f3b6d26 + 47d1587 commit 49d1f6d

7 files changed

Lines changed: 115 additions & 30 deletions

File tree

.changeset/brown-peas-destroy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fix: Astro.site should default to localhost when not provided in a project's config

.changeset/tough-papayas-jam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fix: show "unable to find network to expose" with local network log when using --host without suitable networks

packages/astro/src/core/messages.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,42 +76,44 @@ export function devStart({
7676
const networkLogging = getNetworkLogging(config.server.host);
7777
const toDisplayUrl = (hostname: string) =>
7878
`${https ? 'https' : 'http'}://${hostname}:${port}${rootPath}`;
79-
let addresses = [];
80-
81-
if (networkLogging === 'none') {
82-
addresses = [`${localPrefix}${bold(cyan(toDisplayUrl(localAddress)))}`];
83-
} else if (networkLogging === 'host-to-expose') {
84-
addresses = [
85-
`${localPrefix}${bold(cyan(toDisplayUrl(localAddress)))}`,
86-
`${networkPrefix}${dim('use --host to expose')}`,
87-
];
88-
} else {
89-
addresses = Object.values(os.networkInterfaces())
79+
80+
let local = `${localPrefix}${bold(cyan(toDisplayUrl(localAddress)))}`;
81+
let network = null;
82+
83+
if (networkLogging === 'host-to-expose') {
84+
network = `${networkPrefix}${dim('use --host to expose')}`;
85+
} else if (networkLogging === 'visible') {
86+
const ipv4Networks = Object.values(os.networkInterfaces())
9087
.flatMap((networkInterface) => networkInterface ?? [])
9188
.filter(
9289
(networkInterface) => networkInterface?.address && networkInterface?.family === 'IPv4'
93-
)
94-
.map(({ address }) => {
95-
if (address.includes('127.0.0.1')) {
96-
const displayAddress = address.replace('127.0.0.1', localAddress);
97-
return `${localPrefix}${bold(cyan(toDisplayUrl(displayAddress)))}`;
98-
} else {
99-
return `${networkPrefix}${bold(cyan(toDisplayUrl(address)))}`;
100-
}
101-
})
102-
// ensure Local logs come before Network
103-
.sort((msg) => (msg.startsWith(localPrefix) ? -1 : 1));
90+
);
91+
for (let { address } of ipv4Networks) {
92+
if (address.includes('127.0.0.1')) {
93+
const displayAddress = address.replace('127.0.0.1', localAddress);
94+
local = `${localPrefix}${bold(cyan(toDisplayUrl(displayAddress)))}`;
95+
} else {
96+
network = `${networkPrefix}${bold(cyan(toDisplayUrl(address)))}`;
97+
}
98+
}
99+
if (!network) {
100+
network = `${networkPrefix}${dim('unable to find network to expose')}`;
101+
}
104102
}
105103

106104
const messages = [
107105
`${emoji('🚀 ', '')}${bgGreen(black(` astro `))} ${green(`v${version}`)} ${dim(
108106
`started in ${Math.round(startupTime)}ms`
109107
)}`,
110108
'',
111-
...addresses,
109+
local,
110+
network,
112111
'',
113112
];
114-
return messages.map((msg) => ` ${msg}`).join('\n');
113+
return messages
114+
.filter((msg) => typeof msg === 'string')
115+
.map((msg) => ` ${msg}`)
116+
.join('\n');
115117
}
116118

117119
export function telemetryNotice() {

packages/astro/src/vite-plugin-astro/compile.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ async function compile({
6363
// For Windows compat, prepend the module ID with `/@fs`
6464
pathname: `/@fs${prependForwardSlash(moduleId)}`,
6565
projectRoot: config.root.toString(),
66-
site: config.site ? new URL(config.base, config.site).toString() : undefined,
66+
site: config.site
67+
? new URL(config.base, config.site).toString()
68+
: `http://localhost:${config.server.port}/`,
6769
sourcefile: filename,
6870
sourcemap: 'both',
6971
internalURL: `/@fs${prependForwardSlash(

packages/astro/src/vite-plugin-markdown/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ ${setup}`.trim();
165165
let { code: tsResult } = await transform(astroResult, {
166166
pathname: '/@fs' + prependForwardSlash(fileUrl.pathname),
167167
projectRoot: config.root.toString(),
168-
site: config.site ? new URL(config.base, config.site).toString() : undefined,
168+
site: config.site
169+
? new URL(config.base, config.site).toString()
170+
: `http://localhost:${config.server.port}/`,
169171
sourcefile: id,
170172
sourcemap: 'inline',
171173
// TODO: baseline flag

packages/astro/test/astro-global.test.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ describe('Astro Global', () => {
88
before(async () => {
99
fixture = await loadFixture({
1010
root: './fixtures/astro-global/',
11+
site: 'https://mysite.dev/',
12+
base: '/blog',
1113
});
1214
});
1315

@@ -85,3 +87,74 @@ describe('Astro Global', () => {
8587
});
8688
});
8789
});
90+
91+
describe('Astro Global Defaults', () => {
92+
let fixture;
93+
94+
before(async () => {
95+
fixture = await loadFixture({
96+
root: './fixtures/astro-global/',
97+
});
98+
});
99+
100+
describe('dev', () => {
101+
let devServer;
102+
let $;
103+
104+
before(async () => {
105+
devServer = await fixture.startDevServer();
106+
const html = await fixture.fetch('/blog/?foo=42').then((res) => res.text());
107+
$ = cheerio.load(html);
108+
});
109+
110+
after(async () => {
111+
await devServer.stop();
112+
});
113+
114+
it('Astro.request.url', async () => {
115+
expect($('#pathname').text()).to.equal('');
116+
expect($('#searchparams').text()).to.equal('');
117+
expect($('#child-pathname').text()).to.equal('');
118+
expect($('#nested-child-pathname').text()).to.equal('');
119+
});
120+
});
121+
122+
describe('build', () => {
123+
before(async () => {
124+
await fixture.build();
125+
});
126+
127+
it('Astro.request.url', async () => {
128+
const html = await fixture.readFile('/index.html');
129+
const $ = cheerio.load(html);
130+
131+
expect($('#pathname').text()).to.equal('/');
132+
expect($('#searchparams').text()).to.equal('{}');
133+
expect($('#child-pathname').text()).to.equal('/');
134+
expect($('#nested-child-pathname').text()).to.equal('/');
135+
});
136+
137+
it('Astro.canonicalURL', async () => {
138+
// given a URL, expect the following canonical URL
139+
const canonicalURLs = {
140+
'/index.html': /http:\/\/localhost:\d+\//,
141+
'/post/post/index.html': /http:\/\/localhost:\d+\/post\/post\//,
142+
'/posts/1/index.html': /http:\/\/localhost:\d+\/posts\//,
143+
'/posts/2/index.html': /http:\/\/localhost:\d+\/posts\/2\//,
144+
};
145+
146+
for (const [url, canonicalURL] of Object.entries(canonicalURLs)) {
147+
const html = await fixture.readFile(url);
148+
149+
const $ = cheerio.load(html);
150+
expect($('link[rel="canonical"]').attr('href')).to.match(canonicalURL);
151+
}
152+
});
153+
154+
it('Astro.site', async () => {
155+
const html = await fixture.readFile('/index.html');
156+
const $ = cheerio.load(html);
157+
expect($('#site').attr('href')).to.match(/http:\/\/localhost:\d+\//);
158+
});
159+
});
160+
});

packages/astro/test/fixtures/astro-global/astro.config.mjs

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)