Skip to content

Commit

Permalink
fix(astro): prerendering issue when path contains underscore (#11243)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Jun 14, 2024
1 parent 6582811 commit ba2b14c
Show file tree
Hide file tree
Showing 8 changed files with 95 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 a 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(),
],
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 ba2b14c

Please sign in to comment.