Skip to content
This repository was archived by the owner on Jan 17, 2025. It is now read-only.

Commit 6c14362

Browse files
committed
support load context
1 parent 8736c20 commit 6c14362

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function createWindow() {
8484
8585
## API
8686

87-
### `initRemix({ remixConfig[, mode] })`
87+
### `initRemix({ remixConfig[, mode, getLoadContext] })`
8888

8989
Initializes remix-electron. Returns a promise with a url to load in the browser window.
9090

@@ -94,6 +94,52 @@ Options:
9494

9595
- `mode`: The mode the app is running in. Can be `"development"` or `"production"`. Defaults to `"production"` when packaged, otherwise uses `process.env.NODE_ENV`.
9696

97+
- `getLoadContext`: Use this to inject some value into all of your remix loaders, e.g. an API client. The loaders receive it as `context`
98+
99+
<details>
100+
<summary>Load context TS example</summary>
101+
102+
**app/context.ts**
103+
104+
```ts
105+
import type * as remix from "@remix-run/server-runtime"
106+
107+
// your context type
108+
export type LoadContext = {
109+
secret: string
110+
}
111+
112+
// a custom data function args type to use for loaders/actions
113+
export type DataFunctionArgs = Omit<remix.DataFunctionArgs, "context"> & {
114+
context: LoadContext
115+
}
116+
```
117+
118+
**desktop/main.js**
119+
120+
```ts
121+
const url = await initRemix({
122+
remixConfig,
123+
124+
/** @returns {import("~/context").LoadContext} */
125+
getLoadContext: () => ({
126+
secret: "123",
127+
}),
128+
})
129+
```
130+
131+
In some route file:
132+
133+
```ts
134+
import type { LoadContext } from "~/context"
135+
136+
export async function loader({ context }: DataFunctionArgs) {
137+
// do something with context
138+
}
139+
```
140+
141+
</details>
142+
97143
## Motivation
98144

99145
Electron has [a comprehensive list of security recommendations](https://www.electronjs.org/docs/latest/tutorial/security) to follow when building an app, especially if that app interacts with the web. Which includes, but is not limited to:

src/main.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ export type InitRemixOptions = {
1919
mode?: "development" | "production"
2020
publicFolder?: string
2121
remixConfig: AppConfig
22+
getLoadContext?: (request: Electron.ProtocolRequest) => unknown
2223
}
2324

2425
export async function initRemix({
2526
remixConfig,
2627
mode = defaultMode,
2728
publicFolder = "public",
29+
getLoadContext,
2830
}: InitRemixOptions) {
2931
await app.whenReady()
3032

@@ -43,7 +45,8 @@ export async function initRemix({
4345
return
4446
}
4547

46-
callback(await serveRemixResponse(request, mode, remixConfig))
48+
const context = await getLoadContext?.(request)
49+
callback(await serveRemixResponse(request, mode, remixConfig, context))
4750
} catch (error) {
4851
callback({
4952
statusCode: 500,
@@ -85,6 +88,7 @@ async function serveRemixResponse(
8588
request: Electron.ProtocolRequest,
8689
mode: "development" | "production",
8790
remixConfig: AppConfig,
91+
context?: unknown,
8892
): Promise<Electron.ProtocolResponse> {
8993
const init: RequestInit = {
9094
method: request.method,
@@ -105,7 +109,7 @@ async function serveRemixResponse(
105109

106110
const build = require(serverBuildFolder)
107111
const handleRequest = createRequestHandler(build, {}, mode)
108-
const response = await handleRequest(remixRequest)
112+
const response = await handleRequest(remixRequest, context)
109113

110114
return {
111115
data: Buffer.from(await response.arrayBuffer()),

tests/context.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {} from "electron"
2+
import { test } from "vitest"
3+
4+
test.todo("load context")

0 commit comments

Comments
 (0)