Skip to content

Commit

Permalink
fix: import.meta.env.BASE_URL will be '/' in client loaded component …
Browse files Browse the repository at this point in the history
…on dev mode (#4886)
  • Loading branch information
yuhang-dong authored Oct 4, 2022
1 parent 37cb2fc commit 61d26f3
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-suns-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix: import.meta.env.BASE_URL will be '/' in client loaded component on dev mode
26 changes: 26 additions & 0 deletions packages/astro/e2e/astro-envs.test.js
Original file line number Diff line number Diff line change
@@ -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/')
});
});
1 change: 1 addition & 0 deletions packages/astro/e2e/fixtures/astro-envs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!.env
9 changes: 9 additions & 0 deletions packages/astro/e2e/fixtures/astro-envs/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from 'astro/config';
import vue from '@astrojs/vue';

// https://astro.build/config
export default defineConfig({
site: 'https://example.com',
base: '/blog',
integrations: [vue()],
});
9 changes: 9 additions & 0 deletions packages/astro/e2e/fixtures/astro-envs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/astro-envs",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/vue": "workspace:*",
"astro": "workspace:*"
}
}
13 changes: 13 additions & 0 deletions packages/astro/e2e/fixtures/astro-envs/src/components/Client.vue
Original file line number Diff line number Diff line change
@@ -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>
8 changes: 8 additions & 0 deletions packages/astro/e2e/fixtures/astro-envs/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -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 />
3 changes: 3 additions & 0 deletions packages/astro/src/core/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
);
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 61d26f3

Please sign in to comment.