Skip to content

Commit

Permalink
fix(astro): prerendering issue when path contains underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
V3RON committed Jun 12, 2024
1 parent 1df24a4 commit 2d14a06
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-ravens-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes prerendering issue for libraries in node_modules when a folder with an underscore is in the path.
16 changes: 14 additions & 2 deletions packages/astro/src/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,23 @@ function isInjectedRoute(file: URL, settings: AstroSettings) {
}

function isPublicRoute(file: URL, config: AstroConfig): boolean {
const pagesDir = resolvePages(config);
const parts = file.toString().replace(pagesDir.toString(), '').split('/').slice(1);
const rootDir = config.root.toString();
const pagesDir = resolvePages(config).toString();
const fileDir = file.toString();

// Normalize the file directory path by removing the pagesDir prefix if it exists,
// otherwise remove the rootDir prefix.
const normalizedDir = fileDir.startsWith(pagesDir) ? fileDir.slice(pagesDir.length) : fileDir.slice(rootDir.length);

const parts = normalizedDir
.replace(pagesDir.toString(), '')
.split('/')
.slice(1);

for (const part of parts) {
if (part.startsWith('_')) return false;
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from 'astro/config';
import fakeIntegration from 'fake-astro-library';

export default defineConfig({
integrations: [
fakeIntegration(),
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<title>Project with underscore in the folder name</title>
</head>
<body>
<h1>Testing</h1>
<script>
console.log('hi');
</script>
</body>
</html>
25 changes: 25 additions & 0 deletions packages/astro/test/underscore-in-folder-name.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
import testAdapter from './test-adapter.js';

describe('Projects with a underscore in the folder name', () => {
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/_underscore in folder name/',
output: 'hybrid',
adapter: testAdapter(),
});
await fixture.build();
});

it('includes page from node_modules/fake-astro-library', async () => {
const app = await fixture.loadTestAdapterApp();
/** @type {Set<string>} */
const assets = app.manifest.assets;
assert.equal(assets.has('/index.html'), true);
assert.equal(assets.has('/404.html'), true);
});
});

0 comments on commit 2d14a06

Please sign in to comment.