Skip to content

Make Nunjucks rendering work in the main process#9893

Draft
jackkav wants to merge 2 commits intoKong:developfrom
jackkav:fix/main-process-nunjucks-rendering
Draft

Make Nunjucks rendering work in the main process#9893
jackkav wants to merge 2 commits intoKong:developfrom
jackkav:fix/main-process-nunjucks-rendering

Conversation

@jackkav
Copy link
Copy Markdown
Contributor

@jackkav jackkav commented May 5, 2026

Summary

  • load the browser Nunjucks bundle only in renderer contexts and switch main/inso paths to the Node runtime
  • guard template helper and app-context window usage so template evaluation can run safely outside the renderer
  • add node-environment rendering coverage, including an explicit getRenderedRequestAndContext() regression for the main-process path

Why

getRenderedRequestAndContext() is used from network flows that can execute in Electron main process and inso. This change makes the templating runtime and helper surface safe in those environments.

Validation

  • npm test -w packages/insomnia -- src/templating/__tests__/index.test.ts src/common/__tests__/render.test.ts
  • npm run type-check -w packages/insomnia (blocked by existing generated-type/worktree issues unrelated to this branch)

jackkav and others added 2 commits May 5, 2026 17:08
…texts

- Add nunjucks.node.ts exporting the Node-safe nunjucks bundle
- Update templating/index.ts to dynamically select nunjucks runtime:
  browser bundle in renderer process, Node bundle in main/inso contexts
- Guard window.main.secureReadFile and window.main.openInBrowser in
  base-extension.ts with isRenderer checks (no-op in main process)
- Add isRenderer guards to app.ts getPath, showSaveDialog, and clipboard
  methods that previously called window.* unconditionally
- Add unit test for templating/index in a Vitest Node environment

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings May 5, 2026 15:14
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR makes Insomnia’s templating/Nunjucks rendering usable outside the Electron renderer (Electron main process and inso) by loading the Node Nunjucks runtime in non-renderer contexts and guarding renderer-only helpers so template evaluation doesn’t crash when window APIs aren’t available.

Changes:

  • Add a Node Nunjucks entrypoint and dynamically select the renderer vs Node runtime at render-time.
  • Guard renderer-only helper surfaces (window.main, clipboard, dialogs, paths) to avoid crashes outside the renderer.
  • Add Vitest coverage for Node-environment templating and a regression test for getRenderedRequestAndContext() when process.type indicates the main-process path.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/insomnia/src/templating/nunjucks.node.ts Introduces a Node-runtime Nunjucks export for non-renderer environments.
packages/insomnia/src/templating/index.ts Dynamically loads the correct Nunjucks runtime based on process.type.
packages/insomnia/src/templating/base-extension.ts Guards renderer-only helper calls to allow execution outside the renderer.
packages/insomnia/src/templating/tests/index.test.ts Adds basic Node-environment templating tests.
packages/insomnia/src/plugins/context/app.ts Prevents renderer-only app context operations from running outside renderer.
packages/insomnia/src/common/tests/render.test.ts Adds regression coverage for main-process rendering path via process.type.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

readFile: async (path: string) => {
if (typeof window !== 'undefined' && window.main?.secureReadFile) {
return window.main.secureReadFile({ path });
}
Comment on lines +772 to +775
Object.defineProperty(process, 'type', {
configurable: true,
value: originalProcessType,
});
@jackkav jackkav marked this pull request as draft May 5, 2026 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants