-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix: restore bundled libraries for backend workers #7126
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Persist bundled-library metadata for backend-only workers so state hydration can serialize values that reference libraries included in the frontend build. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Persist the bundled-library registry used by backend-only workers when serializing state hydration data. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -273,6 +273,32 @@ def serialize_initial_value(value: Any) -> Any: | |||||
| ) | ||||||
|
|
||||||
|
|
||||||
| def _compile_bundled_libraries() -> tuple[str, str]: | ||||||
| """Return the bundled-library registry as a frontend build artifact. | ||||||
|
|
||||||
| Returns: | ||||||
| The output path and serialized registry. | ||||||
| """ | ||||||
| bundled_libraries = RegistrationContext.ensure_context().bundled_libraries | ||||||
| return constants.Dirs.BUNDLED_LIBRARIES, format.json_dumps(bundled_libraries) | ||||||
|
|
||||||
|
|
||||||
| def _restore_bundled_libraries() -> None: | ||||||
| """Restore the registry emitted by the most recent frontend compile.""" | ||||||
| path = get_web_dir() / constants.Dirs.BUNDLED_LIBRARIES | ||||||
| try: | ||||||
| bundled_libraries = json.loads(path.read_text(encoding="utf-8")) | ||||||
| except (OSError, json.JSONDecodeError): | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: When Prompt for AI agents
Suggested change
|
||||||
| return | ||||||
| if not isinstance(bundled_libraries, list) or not all( | ||||||
| isinstance(library, str) for library in bundled_libraries | ||||||
| ): | ||||||
| return | ||||||
| RegistrationContext.ensure_context().bundled_libraries[:] = list( | ||||||
| dict.fromkeys(bundled_libraries) | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| def _compile_client_storage_field( | ||||||
| field: Field, | ||||||
| ) -> ( | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,12 @@ | |
| import asyncio | ||
|
|
||
| import pytest | ||
| from reflex_base.registry import RegistrationContext | ||
| from reflex_components_core.base.fragment import Fragment | ||
| from reflex_components_core.base.script import Script | ||
| from reflex_components_core.el.elements.metadata import Link | ||
|
|
||
| from reflex.compiler import utils | ||
| from reflex.compiler.utils import compile_state, create_document_root | ||
| from reflex.compiler.utils import write_file as compiler_write_file | ||
| from reflex.constants.state import FIELD_MARKER | ||
|
|
@@ -20,6 +22,28 @@ def test_write_file_reexport() -> None: | |
| assert compiler_write_file is write_file | ||
|
|
||
|
|
||
| def test_bundled_libraries_artifact_round_trip( | ||
| tmp_path, monkeypatch: pytest.MonkeyPatch | ||
| ) -> None: | ||
| """Backend-only workers can restore the registry from the frontend build.""" | ||
| monkeypatch.setattr(utils, "get_web_dir", lambda: tmp_path) | ||
| with RegistrationContext() as context: | ||
| context.bundled_libraries.append("@radix-ui/themes") | ||
| output_path, output = utils._compile_bundled_libraries() | ||
| (tmp_path / output_path).write_text(output, encoding="utf-8") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test manually writes the registry artifact and directly calls both helpers. It would still pass if Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| context.bundled_libraries[:] = ["react"] | ||
|
|
||
| utils._restore_bundled_libraries() | ||
|
|
||
| assert context.bundled_libraries == [ | ||
| "react", | ||
| "@emotion/react", | ||
| "$/utils/context", | ||
| "$/utils/state", | ||
| "@radix-ui/themes", | ||
| ] | ||
|
|
||
|
|
||
| def test_document_preloads_the_global_stylesheet(): | ||
| """Render-blocking CSS should be discoverable alongside early resource hints.""" | ||
| head = create_document_root().children[0] | ||
|
|
||
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.
P2: When a backend-only worker discovers a library while evaluating a page, this restore replaces that registration with the older frontend snapshot before initial-state serialization. Restore the snapshot before evaluating pages, or merge it with registrations discovered during evaluation, so page-level
bundle_library()calls remain available.Prompt for AI agents