Skip to content

Commit

Permalink
fix: allow overriding browser entrypoint (#273)
Browse files Browse the repository at this point in the history
* fix: allow overriding browser entrypoint

* fix: fmt
  • Loading branch information
deckchairlabs committed Sep 5, 2023
1 parent e58ec93 commit 2bb0261
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"image": "mcr.microsoft.com/devcontainers/base:jammy",
"features": {
"ghcr.io/devcontainers-contrib/features/deno:1": {
"version": "1.35.0"
"version": "1.36.4"
}
},
"customizations": {
Expand Down
23 changes: 13 additions & 10 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@
},
"lock": false,
"lint": {
"files": {
"exclude": [
".deno-cache"
]
}
"exclude": [
".deno-cache"
]
},
"fmt": {
"files": {
"exclude": [
".deno-cache"
]
},
"exclude": [
".deno-cache",
"examples/**/.ultra",
"examples/**/importMap.dev.json",
"examples/**/deno.dev.json",
"examples/with-netlify-(WIP)/.netlify/*",
"examples/ultra-website/src/content/*",
"examples/with-mdx/src/content/*",
"test/fixture/output/*"
],
"options": {
"indentWidth": 2,
"useTabs": false,
Expand Down
2 changes: 1 addition & 1 deletion examples/lite/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ export default function App() {
);
}

typeof (document) !== "undefined" && hydrate(document, <App />);
typeof document !== "undefined" && hydrate(document, <App />);
46 changes: 28 additions & 18 deletions lib/ultra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Context, Env, ImportMap, Mode } from "./types.ts";
import { toUltraUrl } from "./utils/url.ts";

type UltraServerRenderOptions = {
entrypoint?: string;
generateStaticHTML?: boolean;
enableEsModuleShims?: boolean;
disableHydration?: boolean;
Expand Down Expand Up @@ -47,8 +48,6 @@ export class UltraServer<
public entrypoint?: string;
public ultraDir?: string;

#bootstrapModules?: string[];

constructor(
public root: string,
options: UltraServerOptions,
Expand Down Expand Up @@ -97,15 +96,10 @@ export class UltraServer<
*/
if (this.importMap) {
this.#importMapHandler(this.importMap);
this.entrypoint = this.#prepareEntrypoint(this.importMap);

if (this.entrypoint) {
this.#bootstrapModules = [
this.entrypoint.startsWith("file://")
? fromFileUrl(this.entrypoint)
: this.entrypoint,
];
}
this.entrypoint = this.#prepareEntrypoint(
this.importMap,
this.entrypoint,
);
}

// Validate
Expand All @@ -126,14 +120,30 @@ export class UltraServer<
) {
log.debug("Rendering component");

let bootstrapModules: string[] = [];

if (options?.entrypoint) {
options.entrypoint = options?.entrypoint && this.importMap
? this.#prepareEntrypoint(this.importMap, options.entrypoint)
: undefined;
}

const entrypoint = options?.entrypoint || this.entrypoint;

if (entrypoint) {
bootstrapModules = [
entrypoint.startsWith("file://") ? fromFileUrl(entrypoint) : entrypoint,
];
}

return renderToStream(Component, context, {
mode: this.mode,
baseUrl: this.baseUrl,
assetManifest: this.assetManifest,
importMap: this.importMap,
enableEsModuleShims: this.enableEsModuleShims,
esModuleShimsPath: this.esModuleShimsPath,
bootstrapModules: this.#bootstrapModules,
bootstrapModules,
...options,
});
}
Expand Down Expand Up @@ -180,16 +190,16 @@ export class UltraServer<
}
}

#prepareEntrypoint(importMap: ImportMap) {
if (!this.entrypoint) {
#prepareEntrypoint(importMap: ImportMap, entrypoint?: string) {
if (!entrypoint) {
log.debug("No entrypoint provided, hydration disabled.");
return this.entrypoint;
return entrypoint;
}

log.debug(sprintf("Resolving entrypoint: %s", this.entrypoint));
log.debug(sprintf("Resolving entrypoint: %s", entrypoint));

let entrypointSpecifier = `./${
relative(this.root, fromFileUrl(this.entrypoint))
relative(this.root, fromFileUrl(entrypoint))
}`;

if (this.mode === "production") {
Expand All @@ -199,7 +209,7 @@ export class UltraServer<
}
}
} else {
entrypointSpecifier = toUltraUrl(this.root, this.entrypoint, this.mode)!;
entrypointSpecifier = toUltraUrl(this.root, entrypoint, this.mode)!;
}

log.debug(sprintf("Resolved entrypoint: %s", entrypointSpecifier));
Expand Down
6 changes: 6 additions & 0 deletions test/fixture/client.foo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import hydrate from "ultra/hydrate.js";

hydrate(
document,
<div>Something else!</div>,
);
13 changes: 13 additions & 0 deletions test/fixture/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ Deno.test(
assertEquals(content.includes("<strong>Ultra</strong>"), true);
});

await t.step("it can override the entrypoint", async () => {
const response = await server.request("http://localhost/foo");
const content = await response.text();

assertEquals(response.status, 200);
assertEquals(
response.headers.get("content-type"),
"text/html; charset=utf-8",
);

assertEquals(content.includes("client.foo.tsx"), true);
});

await t.step("it can serve static assets from '/public'", async () => {
const response = await server.request("https://localhost/style.css");
response.body?.cancel();
Expand Down
9 changes: 9 additions & 0 deletions test/fixture/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ server.get("*", async (context) => {
// clear query cache
queryClient.clear();

const requestUrl = new URL(context.req.url);

const entrypoint = requestUrl.pathname === "/foo"
? import.meta.resolve("./client.foo.tsx")
: import.meta.resolve("./client.tsx");

/**
* Render the request
*/
Expand All @@ -49,6 +55,9 @@ server.get("*", async (context) => {
<App />
</TWProvider>
</TRPCServerProvider>,
{
entrypoint,
},
);

return context.body(result, 200, {
Expand Down

0 comments on commit 2bb0261

Please sign in to comment.