feat: add support for custom HTTP headers via cfg.customHeaders#21
feat: add support for custom HTTP headers via cfg.customHeaders#21duydvnganluong wants to merge 3 commits intoDigicreon:mainfrom
Conversation
duydvnganluong
commented
Apr 29, 2026
- Support global custom headers via mu.init({customHeaders: {...}})
- Support per-request custom headers in mu.load({customHeaders: {...}})
- Merge custom headers with default µJS headers in both prefetch and main fetch
- Custom headers work with all injection modes and trigger types
- Allows servers to receive custom headers for authentication, tracking, or API-specific requirements
- Support global custom headers via mu.init({customHeaders: {...}})
- Support per-request custom headers in mu.load({customHeaders: {...}})
- Merge custom headers with default µJS headers in both prefetch and main fetch
- Custom headers work with all injection modes and trigger types
- Allows servers to receive custom headers for authentication, tracking, or API-specific requirements
|
Thanks for the contribution! This is a useful and practical addition, and you've kept it nicely minimal at just a handful of lines. Before merging, we'd like to ask for a few adjustments: 1. Scope of the feature This is purely a configuration-level shortcut. It's worth noting that the same effect can already be achieved today through the 2. Rename Since we're already in the µJS config namespace ( 3. Drop the µJS doesn't validate at internal boundaries, only at HTML/user-input boundaries. If a developer passes garbage to 4. Invert the merge order in Currently the user's headers are merged after µJS's own headers, meaning a misconfigured var fetchOpts = {
signal: abortCtrl.signal,
headers: {}
};
if (mu._cfg.headers)
Object.assign(fetchOpts.headers, mu._cfg.headers);
fetchOpts.headers["X-Requested-With"] = "XMLHttpRequest";
fetchOpts.headers["X-Mu-Mode"] = cfg.mode;
// ...This keeps µJS's internal protocol headers safe from accidental override, while still allowing the user to add anything else. 5. Restore the trailing newline The trailing newline at the end of 6. Documentation Please add the new option to Custom headersYou can send additional HTTP headers with every request by setting the mu.init({
headers: {
"Authorization": "Bearer " + myToken,
"Accept-Language": "fr-FR"
}
});These headers are merged into both regular and prefetch requests. µJS's internal headers ( For per-request or dynamic headers (e.g. a fresh CSRF token on each request), listen to the Once these adjustments are in, we'll be happy to merge. Thanks again! |
|
Hi @Amaury |