fix: import.meta.env.BASE_URL will be '/' in client loaded component on dev mode (#4886)

This commit is contained in:
董雨航 2022-10-05 00:57:35 +08:00 committed by GitHub
parent 37cb2fc02a
commit 61d26f3352
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 82 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix: import.meta.env.BASE_URL will be '/' in client loaded component on dev mode

View file

@ -0,0 +1,26 @@
import { expect } from '@playwright/test';
import { testFactory } from './test-utils.js';
const test = testFactory({ root: './fixtures/astro-envs/' });
let devServer;
test.beforeAll(async ({ astro }) => {
devServer = await astro.startDevServer();
});
test.afterAll(async () => {
await devServer.stop();
});
test.describe('Astro Environment BASE_URL', () => {
test('BASE_URL', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/blog/'));
const astroBaseUrl = page.locator('id=astro-base-url');
await expect(astroBaseUrl, 'astroBaseUrl equals to /blog/').toHaveText('/blog/')
const clientComponentBaseUrl = page.locator('id=client-component-base-url');
await expect(clientComponentBaseUrl, 'clientComponentBaseUrl equals to /blog').toHaveText('/blog/')
});
});

View file

@ -0,0 +1 @@
!.env

View file

@ -0,0 +1,9 @@
import { defineConfig } from 'astro/config';
import vue from '@astrojs/vue';
// https://astro.build/config
export default defineConfig({
site: 'http://example.com',
base: '/blog',
integrations: [vue()],
});

View file

@ -0,0 +1,9 @@
{
"name": "@test/astro-envs",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/vue": "workspace:*",
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,13 @@
<template>
<div id="client-component-base-url">{{ BASE_URL }}</div>
</template>
<script>
export default {
data() {
return {
BASE_URL: import.meta.env.BASE_URL,
};
},
};
</script>

View file

@ -0,0 +1,8 @@
---
import Client from '../components/Client.vue';
const {BASE_URL} = import.meta.env
---
<div id="astro-base-url">{BASE_URL}</div>
<Client client:load />

View file

@ -52,6 +52,9 @@ export default async function dev(
optimizeDeps: {
include: rendererClientEntries,
},
define: {
'import.meta.env.BASE_URL': settings.config.base ? `'${settings.config.base}'` : 'undefined',
},
},
{ settings, logging: options.logging, mode: 'dev' }
);

View file

@ -582,6 +582,14 @@ importers:
astro: link:../../..
preact: 10.11.0
packages/astro/e2e/fixtures/astro-envs:
specifiers:
'@astrojs/vue': workspace:*
astro: workspace:*
dependencies:
'@astrojs/vue': link:../../../../integrations/vue
astro: link:../../..
packages/astro/e2e/fixtures/client-only:
specifiers:
'@astrojs/preact': workspace:*