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

Support "base" config of Vite or alternatives #243

Open
berlysia opened this issue Dec 27, 2024 · 3 comments · May be fixed by #252
Open

Support "base" config of Vite or alternatives #243

berlysia opened this issue Dec 27, 2024 · 3 comments · May be fixed by #252
Labels
enhancement New feature or request

Comments

@berlysia
Copy link
Contributor

What is the feature you are proposing?

When client code is present, it is not possible to deploy a site built with HonoX to GitHub Pages.

When deploying a site built with Vite to GitHub Pages, it is instructed to change the base setting.
Behavior for embedded deployments is also provided.

FYI: My workaround and considerations:

  • vite.config.js cannot be read from runtime, so I could not directly tell the Script component the value of base. And the Script component uses "/", so it is not configurable.
  • I have implemented my own Script component equivalent to get around this, but there may be a better way to do it.
    • The base value is shared by environment variables.
@berlysia berlysia added the enhancement New feature or request label Dec 27, 2024
@yusukebe
Copy link
Member

Hi @berlysia

I don't understand your problem 100%, but will it be resolved if you change the path /dist/.vite/manifest.json in this line?

const MANIFEST = import.meta.glob<{ default: Manifest }>('/dist/.vite/manifest.json', {

@berlysia
Copy link
Contributor Author

Hi @yusukebe , thank you for your response.

The problem occurs because the .js file references in each HTML are written as root-relative paths. If the deployment destination is a subpath (e.g., GitHub Pages), the subpath segment must be included when referencing root-relative paths.

When deploying to https://example.com/path/to/some/dir/, the following mismatch arises, causing the .js file to fail to load:

Reference in the output HTML Actual file path = Correct reference
/static/client-${hash}.js /path/to/some/dir/static/client-${hash}.js

Note that .js files themselves are unaffected because they use relative paths.

We will probably do something around manifest.json, but at least we should avoid rewriting manifest.json. We want to keep compatibility with good tools in the Vite ecosystem, right?

This issue can be resolved simply by replacing the hardcoded "/" with import.meta.env.BASE_URL. I will submit a pull request. You can see a sample repository at https://github.com/berlysia/sandbox-honox-base-path (patch).


For embedded deployments (the default behavior in bare Vite when base: ""), the expectation is that we can deploy to any subpath. To achieve this, make sure all paths are written as relative paths rather than root-relative paths.

Empedded deployment improves the reusability of build results, allowing us to build artifacts once and place them in multiple different subpaths.

useRequestContext provides us some helpful information, but it's not enough: we still need to indicate that it’s an embedded deployment, and I couldn’t find a straightforward way to do that immediately 😢.

@berlysia berlysia linked a pull request Jan 11, 2025 that will close this issue
@yusukebe
Copy link
Member

Hi @berlysia

Thank you for the helpful explanation and the PR!

We will probably do something around manifest.json, but at least we should avoid rewriting manifest.json. We want to keep compatibility with good tools in the Vite ecosystem, right?

It's correct. We should not edit a manifest.json directly.

I'm working on reviewing your PR. Please wait a bit!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants