Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fix the use of vite.base in Astro Dev Server #13003

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions .changeset/fresh-badgers-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

The problem solved by this PR is that after setting the base, when pnpm runs dev, the src address of the generated script type="module" tag is incorrect and still starts with/node_modules. It should start with the value of base. I found that modifying the base of root does not work. I need to set vite.base so that it can parse correctly
2 changes: 2 additions & 0 deletions packages/astro/src/container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ function createManifest(
clientDirectives: manifest?.clientDirectives ?? getDefaultClientDirectives(),
renderers: renderers ?? manifest?.renderers ?? [],
base: manifest?.base ?? ASTRO_CONFIG_DEFAULTS.base,
viteBase: manifest?.viteBase ?? '',
componentMetadata: manifest?.componentMetadata ?? new Map(),
inlinedScripts: manifest?.inlinedScripts ?? new Map(),
i18n: manifest?.i18n,
Expand Down Expand Up @@ -228,6 +229,7 @@ type AstroContainerManifest = Pick<
| 'renderers'
| 'assetsPrefix'
| 'base'
| 'viteBase'
| 'routes'
| 'assets'
| 'entryModules'
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type SSRManifest = {
routes: RouteInfo[];
site?: string;
base: string;
viteBase: string | undefined;
trailingSlash: 'always' | 'never' | 'ignore';
buildFormat: 'file' | 'directory' | 'preserve';
compressHTML: boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ function createBuildManifest(
compressHTML: settings.config.compressHTML,
renderers,
base: settings.config.base,
viteBase: settings.config?.vite?.base,
assetsPrefix: settings.config.build.assetsPrefix,
site: settings.config.site,
componentMetadata: internals.componentMetadata,
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/build/plugins/plugin-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ function buildManifest(
routes,
site: settings.config.site,
base: settings.config.base,
viteBase: settings.config?.vite?.base,
trailingSlash: settings.config.trailingSlash,
compressHTML: settings.config.compressHTML,
assetsPrefix: settings.config.build.assetsPrefix,
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ export class RenderContext {
// calling the render() function will populate the object with scripts, styles, etc.
const result: SSRResult = {
base: manifest.base,
viteBase: manifest.viteBase,
cancelled: false,
clientDirectives,
inlinedScripts,
Expand Down
3 changes: 3 additions & 0 deletions packages/astro/src/runtime/server/render/head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export function renderAllHeadContent(result: SSRResult) {
const scripts = Array.from(result.scripts)
.filter(uniqueElements)
.map((script) => {
if(result.viteBase){
script.props.src = (result.base === '/' ? '' : result.base) + result.viteBase + script.props.src;
}
return renderElement('script', script, false);
});
const links = Array.from(result.links)
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/runtime/server/render/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ export async function renderScript(result: SSRResult, id: string) {
}

const resolved = await result.resolve(id);
return markHTMLString(`<script type="module" src="${resolved}"></script>`);
return markHTMLString(`<script type="module" src="${result.viteBase ? ((result.base === '/' ? '' : result.base)+result.viteBase) : ''}${resolved}"></script>`);
}
1 change: 1 addition & 0 deletions packages/astro/src/types/public/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export interface SSRResult {
*/
cancelled: boolean;
base: string;
viteBase: string | undefined;
styles: Set<SSRElement>;
scripts: Set<SSRElement>;
links: Set<SSRElement>;
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/vite-plugin-astro-server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ export function createDevelopmentManifest(settings: AstroSettings): SSRManifest
clientDirectives: settings.clientDirectives,
renderers: [],
base: settings.config.base,
viteBase: settings.config?.vite?.base,
assetsPrefix: settings.config.build.assetsPrefix,
site: settings.config.site,
componentMetadata: new Map(),
Expand Down
34 changes: 34 additions & 0 deletions packages/astro/test/astro-dev-headers.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { load as cheerioLoad } from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Astro dev headers', () => {
Expand Down Expand Up @@ -38,3 +39,36 @@ describe('Astro dev headers', () => {
});
});
});

describe('Astro dev with vite.base path', () => {
let fixture;
let devServer;
const headers = {
'x-astro': 'test',
};

before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-dev-headers/',
server: {
headers,
},
vite: {
base: '/hello'
}
});
await fixture.build();
devServer = await fixture.startDevServer();
});

after(async () => {
await devServer.stop();
});

it('Generated script src get included with vite.base path', async () => {
const result = await fixture.fetch('/hello');
const html = await result.text();
const $ = cheerioLoad(html);
assert.match($('script').attr('src'), /^\/hello\/@vite\/client$/);
});
});
31 changes: 31 additions & 0 deletions pnpm-lock.yaml

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

Loading