Skip to content

Enable HTML handling for HTML files in the public directory #9548

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

Merged
merged 6 commits into from
Jun 13, 2025

Conversation

jamesopstad
Copy link
Contributor

@jamesopstad jamesopstad commented Jun 10, 2025

Fixes #9362.

Enable HTML handling for HTML files in the public directory.

HTML files in the public directory are prioritized over those at the root. This is consistent with Vite's internal behaviour, which places the servePublicMiddleware first.


  • Tests
    • TODO (before merge)
    • Tests included
    • Tests not necessary because:
  • Wrangler / Vite E2E Tests CI Job required? (Use "e2e" label or ask maintainer to run separately)
    • I don't know
    • Required
    • Not required because: N/A
  • Public documentation
    • TODO (before merge)
    • Cloudflare docs PR(s):
    • Documentation not necessary because: N/A
  • Wrangler V3 Backport
    • TODO (before merge)
    • Wrangler PR:
    • Not necessary because: not a Wrangler change

@jamesopstad jamesopstad requested review from a team as code owners June 10, 2025 12:11
@jamesopstad jamesopstad added vite-plugin Relating to the `@cloudflare/vite-plugin` package every-os Run tests (unit/e2e/c3-e2e) on every OS, rather than just macOS labels Jun 10, 2025
Copy link

changeset-bot bot commented Jun 10, 2025

🦋 Changeset detected

Latest commit: a9917bf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cloudflare/vite-plugin Patch

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

@github-project-automation github-project-automation bot moved this to Untriaged in workers-sdk Jun 10, 2025
@jamesopstad jamesopstad added the e2e Run wrangler + vite-plugin e2e tests on a PR label Jun 10, 2025
Copy link

pkg-pr-new bot commented Jun 10, 2025

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@9548

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@9548

miniflare

npm i https://pkg.pr.new/miniflare@9548

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@9548

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@9548

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@9548

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@9548

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@9548

wrangler

npm i https://pkg.pr.new/wrangler@9548

commit: a9917bf

@jamesopstad jamesopstad marked this pull request as draft June 10, 2025 13:32
@jamesopstad jamesopstad force-pushed the james/public-dir-html branch from fc308dd to fcb8930 Compare June 10, 2025 19:08
@jamesopstad jamesopstad marked this pull request as ready for review June 10, 2025 19:38
Copy link
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit of commentary in the code about the PUBLIC_DIR_PREFIX - what it is for and how it is used - would be helpful to future readers. Otherwise LGTM

test("worker configs warnings are not present in the terminal", async () => {
expect(serverLogs.warns).toEqual([]);
expect(serverLogs.warns.join()).not.toContain(
"contains the following configuration options which are ignored since they are not applicable when using Vite"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly fragile test in the sense that any change to the text will cause this test to pass due to a false positive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, agree it's not ideal. Had to change this as one of the tests does trigger a warning. There's a corresponding test in another variant that tests the opposite.


return exists ? pathname : null;
return response.json() as Promise<string | null>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT:

Suggested change
return response.json() as Promise<string | null>;
return response.json<string|null>();

@@ -48,3 +48,7 @@ export type Defined<T> = Exclude<T, undefined>;
export function getFirstAvailablePort(start: number) {
return getPort({ port: portNumbers(start, 65535) });
}

export function withTrailingSlash(path: string): string {
return path[path.length - 1] !== "/" ? `${path}/` : path;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT:

Suggested change
return path[path.length - 1] !== "/" ? `${path}/` : path;
return path.endsWith("/") ? path : `${path}/`;

@github-project-automation github-project-automation bot moved this from Untriaged to Approved in workers-sdk Jun 11, 2025
@jamesopstad jamesopstad force-pushed the james/public-dir-html branch from fcb8930 to b6711b4 Compare June 12, 2025 19:44
@jamesopstad jamesopstad enabled auto-merge June 12, 2025 19:46
@jamesopstad jamesopstad disabled auto-merge June 12, 2025 19:47
Copy link
Contributor

@IgorMinar IgorMinar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome! Thank you James for getting this working.

@jamesopstad jamesopstad force-pushed the james/public-dir-html branch from b6711b4 to a9917bf Compare June 13, 2025 07:23
@jamesopstad jamesopstad added this pull request to the merge queue Jun 13, 2025
Merged via the queue into main with commit 0174e39 Jun 13, 2025
29 of 34 checks passed
@jamesopstad jamesopstad deleted the james/public-dir-html branch June 13, 2025 08:28
@github-project-automation github-project-automation bot moved this from Approved to Done in workers-sdk Jun 13, 2025
jseba added a commit to jseba/workers-sdk that referenced this pull request Jun 14, 2025
* 'main' of ssh://github.com/cloudflare/workers-sdk: (213 commits)
  fix(vite-plugin-cloudflare): allow unknown entrypoint on startup (cloudflare#9583)
  add warning about env not specified to potentially risky wrangler commands (cloudflare#9344)
  test: correctly delete test d1 databases (cloudflare#9598)
  Add details to v3 maintenance worflow error message (cloudflare#9602)
  drop unused `WRANGLER_UNENV_RESOLVE_PATHS` env var (cloudflare#9593)
  warn instead of erroring if no exposed ports. and remove unused dev_exposed_ports (cloudflare#9573)
  fix(wrangler): point to the right doc location (replaces cloudflare#9353) (cloudflare#9592)
  [vitest-pool-worker] Add `type: module` in package.json (cloudflare#9595)
  Enable HTML handling for HTML files in the public directory (cloudflare#9548)
  switch from go container to js (cloudflare#9588)
  containers: switch to Cloudflare container registry (cloudflare#9564)
  performance improvement: restart a mixed mode session only if the worker's remote bindings have changed (cloudflare#9536)
  ci: delete more orphaned resources from e2e tests (cloudflare#9580)
  test: remove `verifyBuildScript` logic from c3 e2e tests (cloudflare#9504)
  fix(vite-plugin): ensure node compat globals are injected before any imports (cloudflare#9581)
  feat(containers-shared): Move `isDockerfile` utility function to `containers-shared` (cloudflare#9577)
  allow `startWorker` to accept `false` as an `inspector` option (to disable the inspector server) (cloudflare#9550)
  Update capnp generated code from the workerd.capnp (cloudflare#9582)
  Bump `@cloudflare/unenv-preset` to 2.3.3 (cloudflare#9566)
  test: make custom build bundling tests more robust (cloudflare#9494)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
e2e Run wrangler + vite-plugin e2e tests on a PR every-os Run tests (unit/e2e/c3-e2e) on every OS, rather than just macOS vite-plugin Relating to the `@cloudflare/vite-plugin` package
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

inconsistencies with routing patterns with single page app mode (wrangler/vite)
3 participants