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

Merged
merged 24 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c50081a
feat: Dev mode supports reverse proxy of nginx subdirectories
chaegumi Jan 18, 2025
5ff4533
feat: type define
chaegumi Jan 18, 2025
1c26ce5
feat: type define update
chaegumi Jan 18, 2025
328e2e2
feat: type define update
chaegumi Jan 18, 2025
d11700c
feat: base use update
chaegumi Jan 18, 2025
9d22d7a
feat: check obj value
chaegumi Jan 19, 2025
016d842
feat: change patch
chaegumi Jan 19, 2025
837f89b
feat: fix test
chaegumi Jan 20, 2025
7c5ab7f
feat: update type and update changeset
chaegumi Jan 21, 2025
cf6eef8
feat: add test
chaegumi Jan 22, 2025
0930a6c
feat: rewrite test
chaegumi Jan 22, 2025
f3abfc6
feat: update config
chaegumi Jan 24, 2025
b0fb0c7
feat: update use pipeline.manifest
chaegumi Jan 28, 2025
5d15cf6
feat: update viteBase type and default value
chaegumi Jan 28, 2025
fefa47f
Merge remote-tracking branch 'origin/main' into nginx-proxy_pass-dev-…
chaegumi Feb 11, 2025
0ddfe33
chore: update package
chaegumi Feb 11, 2025
95d63ee
Merge remote-tracking branch 'origin/main' into nginx-proxy_pass-dev-…
chaegumi Feb 12, 2025
8ca91d9
Merge remote-tracking branch 'origin/main' into nginx-proxy_pass-dev-…
chaegumi Feb 12, 2025
7d9f7dd
chore: install package
chaegumi Feb 12, 2025
8106bb4
Merge remote-tracking branch 'origin/main' into nginx-proxy_pass-dev-…
chaegumi Feb 14, 2025
00f3d0b
chore: install package
chaegumi Feb 14, 2025
9b58fe3
chore finish refactor
ematipico Feb 24, 2025
04f39d2
Update changeset
ascorbic Feb 24, 2025
483aa69
Merge branch 'main' into nginx-proxy_pass-dev-mode
ascorbic Feb 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
---

Fixes a bug that caused the `vite.base` value to be ignored when running `astro dev`
2 changes: 2 additions & 0 deletions packages/astro/src/container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ function createManifest(
clientDirectives: manifest?.clientDirectives ?? getDefaultClientDirectives(),
renderers: renderers ?? manifest?.renderers ?? [],
base: manifest?.base ?? ASTRO_CONFIG_DEFAULTS.base,
userAssetsBase: manifest?.userAssetsBase ?? '',
componentMetadata: manifest?.componentMetadata ?? new Map(),
inlinedScripts: manifest?.inlinedScripts ?? new Map(),
i18n: manifest?.i18n,
Expand Down Expand Up @@ -234,6 +235,7 @@ type AstroContainerManifest = Pick<
| 'renderers'
| 'assetsPrefix'
| 'base'
| 'userAssetsBase'
| 'routes'
| 'assets'
| 'entryModules'
Expand Down
7 changes: 7 additions & 0 deletions packages/astro/src/core/app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ export type SSRManifest = {
routes: RouteInfo[];
site?: string;
base: string;
/**
* The base of the assets generated **by the user**. For example, scripts created by the user falls under this category.
*
* The value of this field comes from `vite.base`. We aren't usually this tight to vite in our code base, so probably
* this should be refactored somehow.
*/
userAssetsBase: string | undefined;
trailingSlash: AstroConfig['trailingSlash'];
buildFormat: NonNullable<AstroConfig['build']>['format'];
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 @@ -630,6 +630,7 @@ function createBuildManifest(
compressHTML: settings.config.compressHTML,
renderers,
base: settings.config.base,
userAssetsBase: 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 @@ -287,6 +287,7 @@ function buildManifest(
routes,
site: settings.config.site,
base: settings.config.base,
userAssetsBase: 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 @@ -394,6 +394,7 @@ export class RenderContext {
// calling the render() function will populate the object with scripts, styles, etc.
const result: SSRResult = {
base: manifest.base,
userAssetsBase: manifest.userAssetsBase,
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.userAssetsBase){
script.props.src = (result.base === '/' ? '' : result.base) + result.userAssetsBase + 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.userAssetsBase ? ((result.base === '/' ? '' : result.base)+result.userAssetsBase) : ''}${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;
userAssetsBase: 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 @@ -190,6 +190,7 @@ export function createDevelopmentManifest(settings: AstroSettings): SSRManifest
clientDirectives: settings.clientDirectives,
renderers: [],
base: settings.config.base,
userAssetsBase: 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$/);
});
});
Loading