Merge some standalone Vite entries into index.js#37085
Conversation
The webpack-to-vite migration broke OpenAPI document rendering in dev mode because viteDevSourceURL blindly mapped all css/* paths to /web_src/css/*, but standalone CSS like swagger.css lives in web_src/css/standalone/ and needs its bundled npm dependencies. Replace the blind css/ mapping with a generic file-existence check that works for both CSS and JS. Consolidate JS if-chains into a switch and add a path traversal guard. For theme support, load the user's Gitea theme CSS on the /api/swagger page so swagger.ts can read --is-dark-theme instead of relying on prefers-color-scheme (which ignores the user's Gitea theme preference). For the iframe case, inject --is-dark-theme and --color-box-body from the parent page into the iframe document on load. Hide the iframe with is-loading until content is ready to prevent color flashes. Co-Authored-By: Claude (Opus 4.6) <noreply@anthropic.com>
|
Why not simply merge standalone scripts into the one |
|
I'm not sure that is actual "simpler". |
You will only have one index.js entry, no need to play the tricks with the files in |
|
Nope. Although I suggested to merge them, you corrected me that it won't work due to I mean merage "devtest" and "swagger" into "index.ts", then they can share the modules, no more vite configs, and maybe fewer chunks. Since every module in index is chunked, so nothing worse? |
|
They won't run since these JS won't be loaded on the devtest or swagger page, we can make them lazy-load? |
|
I'm talking about all the other |
|
The only real benefit is deduplication of the big swagger-ui chunk which could be lazy-loaded in two places instead of currently one, saving probably ~200kB output size. I guess I need to experiment if the extra round-trip is noticable in practice or not. |
I have added the benchmark, they won't take much time. Most of them do nothing if there is no related element on the page. And we can migrate them to our data-global-init framework, then maybe can be faster.
You can benchmark, less than 0.0...01s |
I don't think anyone can really feel the " 1 extra round trip " on the devtest / swagger page, and I don't think anyone would be affected. |
|
If your latency to the server is 300ms+, it could be noticable. But generally most users will have less latency than that. |
|
In this case, the more chunks in "index.js" (caused by the standalone files) will affect users more, for >99% time they are visiting the main site's pages, but not swagger page. So even the network latency is high, the merged solution still brings best user experience. |
… resize during loading - Add matchMedia change listener so swagger dark-mode class updates when system color scheme changes at runtime (relevant for auto theme) - Remove is-loading guard on iframe resize messages so the correct height is set before the iframe becomes visible Co-Authored-By: Claude (Opus 4.6) <noreply@anthropic.com>
|
It seems we have to load iife (needed for jQuery, which is needed by fomantic import in index.js), so it will be loading considerably more js on those pages, but I guess it wont matter. |
|
Remove standalone JS entry points (swagger, devtest, external-render-iframe) from the Vite config. Move their init logic into modules/ and register them in index.ts's callInitFunctions array. Standalone pages (openapi iframe, external render iframe) now load iife.js + index.js via head_script template. - Move swagger/devtest/external-render-iframe JS into modules/ - Delete web_src/js/standalone/ and web_src/css/standalone/ - Keep swagger.css and devtest.css as CSS-only Vite entries - Swagger template and openapi.go use head_script + index.js - Pass pre-rendered HeadScriptHTML from router to markup renderer - Use `isDarkTheme()` with matchMedia fallback for swagger dark mode - Use `data-gitea-theme-dark` attribute for swagger CSS overrides - Use CSS vars instead of hardcoded colors in swagger.css - Guard customElements.define to prevent re-registration in iframes - Suppress context-canceled proxy errors in vitedev.go Co-Authored-By: Claude (Opus 4.6) <noreply@anthropic.com>
Co-Authored-By: Claude (Opus 4.6) <noreply@anthropic.com>
|
Standalone entry points removed. CSS files are kept as entry points to avoid FOUC, but could also make it load |
During the refactoring, I found that keeping In the future, if the swagger ui page needs to use main site's modules, we can migrate it at that time (and the newly added HINT comment can help people to understand the problem). |
|
Found another problem: inconsistent output directories for JS Old:
To make everything clear and consistent, now, all "entry" files are moved to the If any one would like to add sub-directories in the future, all the paths must be kept consistent: if an entry is in a sub-directory, its output should also be in the same sub-directory, and the frontend should also use the sub-directory to import it. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 32 out of 34 changed files in this pull request and generated 3 comments.
Comments suppressed due to low confidence (2)
web_src/js/external-render-helper.ts:23
- data-gitea-theme-dark is only set when the query param indicates dark mode. When it’s false, the attribute is left unset, which breaks CSS selectors that rely on explicit "true"/"false" (e.g. GitHub-style dark/light image switching based on html[data-gitea-theme-dark="false"]). Consider always setting the attribute to "true" or "false" based on the param value.
web_src/js/external-render-helper.ts:32 - themeUri is taken directly from a query parameter and injected into a . A crafted /render/ URL could point this to an arbitrary external CSS URL, causing the sandboxed iframe to make cross-origin requests (privacy leak via Referer) and allowing untrusted CSS to be applied. Consider validating themeUri before using it (e.g. only allow same-origin /assets/css/theme-.css (and/or /web_src/css/themes/ in dev), otherwise ignore).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
I have tested:
X a. embedded swagger X A. light theme Will merge if tests pass, to catch 1.26 release. |
* main: Repair duration display for bad stopped timestamps (go-gitea#37121) Add terraform state registry (go-gitea#36710) Add placeholder content for empty content page (go-gitea#37114) Improve control char rendering and escape button styling (go-gitea#37094) Add gpg signing for merge rebase and update by rebase (go-gitea#36701) Move package settings to package instead of being tied to version (go-gitea#37026) Merge some standalone Vite entries into index.js (go-gitea#37085) Update Nix flake (go-gitea#37110) [skip ci] Updated translations via Crowdin Fix the wrong push commits in the pull request when force push (go-gitea#36914)
|
Why is CORS being enabled in the Vite server? Surely we can do without CORS? |
Surely you can't. |
Keep
swaggerandexternal-render-helperas a standalone entries for external render.devtest.tstomodules/as init functionsThis PR was written with the help of Claude Opus 4.6