Repository navigation
dev server: give the page of an HTML route one file of a package with "main" and "module" #46994
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
Closed
robobun
wants to merge
3
commits into
robobun/2b11fba7/dev-server-esmodule-default
from
robobun/af7f8ee3/dev-server-route-forward
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| //! The dev server's stand-in for `BundleV2::scan_for_secondary_paths`: the "main" file behind each import of a "module" file. | ||
|
|
||
| use bun_ast::{ImportRecord, ImportRecordFlags}; | ||
| use bun_collections::StringHashMap; | ||
|
|
||
| use crate::bun_fs as Fs; | ||
| use crate::bundle_v2::BundleV2; | ||
|
|
||
| /// The secondary paths of a dev server bundle, by primary path. | ||
| #[derive(Default)] | ||
| pub struct SecondaryPaths { | ||
| by_primary: StringHashMap<&'static [u8]>, | ||
| } | ||
|
|
||
| impl SecondaryPaths { | ||
| /// For an import record that the dev server resolved for a client file. The record is bound to `primary`. | ||
| #[inline] | ||
| pub(crate) fn note( | ||
| &mut self, | ||
| record: &mut ImportRecord, | ||
| resolved: &bun_resolver::Result, | ||
| primary: &Fs::Path<'_>, | ||
| ) { | ||
| let Some(secondary) = &resolved.path_pair.secondary else { | ||
| return; | ||
| }; | ||
| if secondary.is_disabled || bun_core::strings::eql_long(secondary.text, primary.text, true) | ||
| { | ||
| return; | ||
| } | ||
| record.flags.insert(ImportRecordFlags::HAS_SECONDARY_PATH); | ||
| bun_core::handle_oom(self.by_primary.put(primary.text, secondary.text)); | ||
| } | ||
|
|
||
| /// The secondary path of an import record with `HAS_SECONDARY_PATH` that is bound to `primary`. | ||
| pub fn get(&self, primary: &[u8]) -> Option<&'static [u8]> { | ||
| self.by_primary.get(primary).copied() | ||
| } | ||
|
|
||
| /// Each primary path of `get` with its secondary path. | ||
| pub fn iter(&self) -> impl Iterator<Item = (&[u8], &'static [u8])> { | ||
| self.by_primary | ||
| .iter() | ||
| .map(|(primary, secondary)| (&**primary, *secondary)) | ||
| } | ||
| } | ||
|
|
||
| impl<'a> BundleV2<'a> { | ||
| pub fn dev_server_secondary_paths(&self) -> &SecondaryPaths { | ||
| &self.dev_secondary_paths | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🟣 pre-existing, not blocking: A developer who adds a
require()of a package the open page already imports gets two copies of it after the hot update, until a manual reload that nothing prompts. The base fails the same way (pre-existing), but this is the hazard the change sets out to fix. At finalize.rs:963-980 the route is hot-updated with the stalehtml.forwards; the HMR chunk carries no forwards, so the page keeps the "module" file while the new require loads "main". Fix: when a bundle changes what an html route forwards (compare the route's newfind_forwardswithhtml.forwards), list the route for a reload instead of a hot update, so an open page never runs both files.A small fix can ride a push you are already making; otherwise a short reply is enough.
Why this was flagged
A page of an HTML route is open with index.js doing
import moment from "moment"and no file requiring it, so the chunk has no forwards (hmr-module.ts:39 readsconfig.forwardsonce at load). The user saves other.js addingrequire("moment"). src/runtime/bake/dev_server/finalize.rs:963-980 only recomputes the stylesheet list with the old html.forwards, and the HMR chunk at finalize.rs:1074 ships other.js plus the newly parsed moment "main" file with no forwards. In the browser loadModuleSync at hmr-module.ts:320 finds no forward for the main id and loads it as a second instance, while index.js keeps the "module" instance. The page is now in the dual-package state (moment.tz is not a function) and stays there: no update puts the route in List 1 (finalize.rs:840-848), which is what makes hmr-runtime-client.ts:198 call performRouteReload/fullReload. The base branch always runs two copies, so the state is not worse than base, but the PR's own page load would give one copy.Verification: pre-existing; acknowledged in diff. /home/claude/bun/src/runtime/bake/hmr-module.ts:39 reads the map once from the initial-response config; the hot-update chunk at /home/claude/bun/src/runtime/bake/dev_server/finalize.rs:1071-1079 has
forwards: &[], and finalize.rs:963-969 only recomputes CSS via the stalehtml.forwards. test/bake/dev/bundle.test.ts:2050-2060 pins exactly this.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.
Not in this PR. This PR makes a page load bind one file. It leaves an open page as the base has it until its next load (Downsides, and "What still differs from a build" in the notes). The reload that you describe is the follow-up that the notes name. It is more than the compare:
/admin/*beside/*) then loads again for the other route and not for its own. dev server: tell a page that loaded an outdated client bundle to reload #42041 works on the reload of a page with an outdated client bundle.hmr-runtime-client.ts.If a maintainer wants it in this PR, I can add it here.