Skip to content

Commit ba2b14c

Browse files
V3RONmatthewpematipico
authoredJun 14, 2024··
fix(astro): prerendering issue when path contains underscore (#11243)
* fix(astro): prerendering issue when path contains underscore * chore: add missing files * Update .changeset/honest-ravens-double.md Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> --------- Co-authored-by: Matthew Phillips <matthew@matthewphillips.info> Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
1 parent 6582811 commit ba2b14c

File tree

8 files changed

+95
-2
lines changed

8 files changed

+95
-2
lines changed
 

‎.changeset/honest-ravens-double.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro": patch
3+
---
4+
5+
Fixes a prerendering issue for libraries in `node_modules` when a folder with an underscore is in the path.

‎packages/astro/src/core/util.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,23 @@ function isInjectedRoute(file: URL, settings: AstroSettings) {
120120
}
121121

122122
function isPublicRoute(file: URL, config: AstroConfig): boolean {
123-
const pagesDir = resolvePages(config);
124-
const parts = file.toString().replace(pagesDir.toString(), '').split('/').slice(1);
123+
const rootDir = config.root.toString();
124+
const pagesDir = resolvePages(config).toString();
125+
const fileDir = file.toString();
126+
127+
// Normalize the file directory path by removing the pagesDir prefix if it exists,
128+
// otherwise remove the rootDir prefix.
129+
const normalizedDir = fileDir.startsWith(pagesDir) ? fileDir.slice(pagesDir.length) : fileDir.slice(rootDir.length);
130+
131+
const parts = normalizedDir
132+
.replace(pagesDir.toString(), '')
133+
.split('/')
134+
.slice(1);
135+
125136
for (const part of parts) {
126137
if (part.startsWith('_')) return false;
127138
}
139+
128140
return true;
129141
}
130142

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineConfig } from 'astro/config';
2+
import fakeIntegration from 'fake-astro-library';
3+
4+
export default defineConfig({
5+
integrations: [
6+
fakeIntegration(),
7+
],
8+
});

‎packages/astro/test/fixtures/_underscore in folder name/node_modules/fake-astro-library/404.astro

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/astro/test/fixtures/_underscore in folder name/node_modules/fake-astro-library/index.ts

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/astro/test/fixtures/_underscore in folder name/node_modules/fake-astro-library/package.json

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<html>
2+
<head>
3+
<title>Project with underscore in the folder name</title>
4+
</head>
5+
<body>
6+
<h1>Testing</h1>
7+
<script>
8+
console.log('hi');
9+
</script>
10+
</body>
11+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import assert from 'node:assert/strict';
2+
import { before, describe, it } from 'node:test';
3+
import { loadFixture } from './test-utils.js';
4+
import testAdapter from './test-adapter.js';
5+
6+
describe('Projects with a underscore in the folder name', () => {
7+
let fixture;
8+
9+
before(async () => {
10+
fixture = await loadFixture({
11+
root: './fixtures/_underscore in folder name/',
12+
output: 'hybrid',
13+
adapter: testAdapter(),
14+
});
15+
await fixture.build();
16+
});
17+
18+
it('includes page from node_modules/fake-astro-library', async () => {
19+
const app = await fixture.loadTestAdapterApp();
20+
/** @type {Set<string>} */
21+
const assets = app.manifest.assets;
22+
assert.equal(assets.has('/index.html'), true);
23+
assert.equal(assets.has('/404.html'), true);
24+
});
25+
});

0 commit comments

Comments
 (0)
Please sign in to comment.