Skip to content

Commit

Permalink
Fix injected scripts not injected to injected routes (#7262)
Browse files Browse the repository at this point in the history
* Fix injected scripts not injected to injected routes

* chore: changeset

---------

Co-authored-by: Nate Moore <[email protected]>
  • Loading branch information
andremralves and natemoo-re committed Jun 6, 2023
1 parent 67c8f34 commit 144813f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/orange-dryers-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix injected scripts not injected to injected routes
9 changes: 8 additions & 1 deletion packages/astro/src/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ function isInPagesDir(file: URL, config: AstroConfig): boolean {
return file.toString().startsWith(pagesDir.toString());
}

function isInjectedRoute(file: URL, settings: AstroSettings) {
for (const route of settings.injectedRoutes) {
if(file.toString().endsWith(route.entryPoint)) return true;
}
return false;
}

function isPublicRoute(file: URL, config: AstroConfig): boolean {
const pagesDir = resolvePages(config);
const parts = file.toString().replace(pagesDir.toString(), '').split('/').slice(1);
Expand All @@ -127,7 +134,7 @@ function endsWithPageExt(file: URL, settings: AstroSettings): boolean {
}

export function isPage(file: URL, settings: AstroSettings): boolean {
if (!isInPagesDir(file, settings.config)) return false;
if (!isInPagesDir(file, settings.config) && !isInjectedRoute(file, settings)) return false;
if (!isPublicRoute(file, settings.config)) return false;
return endsWithPageExt(file, settings);
}
Expand Down
30 changes: 30 additions & 0 deletions packages/astro/test/astro-scripts.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
import { tailwind } from './fixtures/astro-scripts/deps.mjs';

describe('Scripts (hoisted and not)', () => {
describe('Build', () => {
Expand Down Expand Up @@ -139,6 +140,21 @@ describe('Scripts (hoisted and not)', () => {
before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-scripts/',
integrations: [
tailwind(),
{
name: 'test-script-injection-with-injected-route',
hooks: {
'astro:config:setup': ({ injectRoute, injectScript }) => {
injectScript(
'page',
`import '/src/scripts/something.js';`
);
injectRoute({ pattern: 'injected-route', entryPoint: 'src/external-page.astro' });
},
},
}
],
vite: {
build: {
assetsInlineLimit: 0,
Expand Down Expand Up @@ -182,5 +198,19 @@ describe('Scripts (hoisted and not)', () => {
});
expect(found).to.equal(1);
});

it('Injected scripts are injected to injected routes', async () => {
let res = await fixture.fetch('/injected-route');
let html = await res.text();
let $ = cheerio.load(html);
let found = 0;
let moduleScripts = $('[type=module]');
moduleScripts.each((i, el) => {
if ($(el).attr('src').includes('@id/astro:scripts/page.js')) {
found++;
}
});
expect(found).to.equal(1);
});
});
});
2 changes: 2 additions & 0 deletions packages/astro/test/fixtures/astro-scripts/deps.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as tailwind } from '@astrojs/tailwind';

11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/astro-scripts/src/external-page.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
---

<html lang="en">
<head>
<title>External page</title>
</head>
<body>
<h1>My external page</h1>
</body>
</html>

0 comments on commit 144813f

Please sign in to comment.