-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Allowing Vite to handle base config for deploying to subpaths #3178
Conversation
🦋 Changeset detectedLatest commit: 6824dd3 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Can you explain the Astro.request.url change more? I assume you mean in dev? Are you saying that: Current main, with subpath
It is now this? This branch
|
Yep that's exactly right. Today in main, There might be a way we could shim the subpath back into the request URL, I'm open to that if it's needed but didn't find a clean fix yet and it might actually make more sense for the basepath to not be included in |
I guess this (using vite for handling base directly) would also make it possible to have all assets and links relative like in this example:
this works when using vite directly, here on the example of a vite svelte package (the export default defineConfig({
plugins: [svelte()],
base: './'
}) and wenn building the site with: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="./favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Svelte + Vite App</title>
<script type="module" crossorigin src="./assets/index.8a09354a.js"></script>
<link rel="stylesheet" href="./assets/index.cf80e917.css">
</head>
<body>
<div id="app"></div>
</body>
</html> compared to the current astro.build version where it doesn't work, if I set it to './' or just empty string in export default defineConfig({
integrations: [
tailwind()
],
base: './',
}); I get absoulte paths in the output: <link rel="stylesheet" href="/assets/asset.a7084969.css"></link> |
18cf3fb
to
be3723e
Compare
astroConfig.base !== '/' | ||
? joinPaths(astroConfig.site?.toString() || 'http://localhost/', astroConfig.base) | ||
: astroConfig.site; | ||
const site = astroConfig.base !== '/' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sure that the subpath is always used for scripts and styles that Astro injects into the page
@@ -162,7 +162,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp | |||
root: viteConfig.root, | |||
envPrefix: 'PUBLIC_', | |||
server: viteConfig.server, | |||
base: astroConfig.site ? new URL(astroConfig.site).pathname : '/', | |||
base: astroConfig.base, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This gives Vite the base URL (or the "/" default), making sure that any imported assets include the subpath
i.e. import image from "../images/penguin.png"
will resolve to /subpath/assets/asset.123.png
a363133
to
3de7685
Compare
…)" This reverts commit 637919c.
3de7685
to
3559225
Compare
@@ -96,6 +96,9 @@ describe('Static build', () => { | |||
for (const link of links) { | |||
const href = $(link).attr('href'); | |||
|
|||
// The imported .scss file should include the base subpath in the href | |||
expect(href.startsWith('/subpath/')).to.be.true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test had a coverage bug in it that ignored the fact that "/subpath/" wasn't being used at all before this PR
This makes sure the test fixture's ESM import for a .scss
file gets prepended with the subpath by Vite's build
…tro#3178) * Revert "Improvements to build and dev when building for subpaths (withastro#3156)" This reverts commit 637919c. * letting Vite handle base paths * test updates to expect Astro.request.url to no longer include subpaths * bringing back the fix for including subpaths in injects scripts and styles * fixing the static-build test to handle subpaths for injected CSS * fixing asset import URLs when using base subpaths * chore: fixing typo in the comments * Astro needs to manage base in dev to maintain Astro.request.url * fix: reverting dev routing tests to expect existing behavior * reverting Astro global test to verify existing behavior * chore: adding changeset * test: update static-build tests to verify the subpath is used in asset imports
…tro#3178) * Revert "Improvements to build and dev when building for subpaths (withastro#3156)" This reverts commit 637919c. * letting Vite handle base paths * test updates to expect Astro.request.url to no longer include subpaths * bringing back the fix for including subpaths in injects scripts and styles * fixing the static-build test to handle subpaths for injected CSS * fixing asset import URLs when using base subpaths * chore: fixing typo in the comments * Astro needs to manage base in dev to maintain Astro.request.url * fix: reverting dev routing tests to expect existing behavior * reverting Astro global test to verify existing behavior * chore: adding changeset * test: update static-build tests to verify the subpath is used in asset imports
Changes
Follow-up to #3156, this gets Astro out of the subpath game completely and lets Vite take care of it
The fix here ended up being pretty straight forward, though not totally ideal since we're still handling the
base
config option ourself in devastro dev
The main challenge in development is that we really need
Astro.request.url
to include the base path, but if Vite is handling thebase
config it will remove that from the request. i.e. requesting/subpath/images/penguin.png
in dev will gives us a URL of/images/penguin.png
This would cause an issue especially when comparing the URL for scenarios like highlighting the current navigation menu item.
astro build
In production builds, this now lets Vite handle the
base
config. This is necessary to make sure the base path is included for imported assets. i.e.import Penguin from "../images/penguin.png"
needs to resolve as/subpath/assets/asset.123.png
For scripts and styles that Astro injects, this change updates the build to include the subpath there as well
Testing
Existing tests mostly cover this already, the static-build suite actually had a bug in one test that was letting it pass today even though the subpath wasn't being included in the build
Docs
None, bug fix only