Skip to content

Instantly share code, notes, and snippets.

View jacob-ebey's full-sized avatar

Jacob Ebey jacob-ebey

View GitHub Profile
@jacob-ebey
jacob-ebey / recursive-rsc-stream-inline.ts
Last active March 25, 2024 07:05
Inline RSC stream with a recursive async component that is streamed as part of the SSR renderToXYZ call.
import * as React from "react";
// @ts-ignore
import * as ReactDOM from "#react-dom-server-implementation";
// @ts-ignore
import * as ReactDOMClient from "#react-server-dom-client-implementation";
export async function fetch(
request: Request,
{
browserEntry,
@jacob-ebey
jacob-ebey / image.ts
Last active February 29, 2024 05:25
Remix Image Component
import { createHash } from "crypto";
import fs from "fs";
import fsp from "fs/promises";
import path from "path";
import https from "https";
import { PassThrough } from "stream";
import type { Readable } from "stream";
import type { LoaderFunction } from "remix";
import sharp from "sharp";
import type { Request as NodeRequest } from "@remix-run/node";
@jacob-ebey
jacob-ebey / federated-rsc-stream
Last active January 9, 2024 18:48
example-federated-rsc-stream
1:I["rsc/remote/client/_example_consumer:./__/__/framework/dist/framework.client.js#OutletProvider",["rsc/remote/client/_example_consumer:./__/__/framework/dist/framework.client.js#OutletProvider"],"OutletProvider"]
2:I["rsc/remote/client/_example_consumer:./app/components/counter.tsx#Counter",["rsc/remote/client/_example_consumer:./app/components/counter.tsx#Counter"],"Counter"]
5:I["rsc/remote/client/_example_consumer:./__/__/framework/dist/framework.client.js#Outlet",["rsc/remote/client/_example_consumer:./__/__/framework/dist/framework.client.js#Outlet"],"Outlet"]
0:["$","$L1",null,{"outlets":{"_example_consumer/_public._index":[["$","h1",null,{"children":"Hello Index"}],["$","$L2",null,{}],["$","div",null,{"style":{"backgroundColor":"red","color":"black","padding":"1rem"},"children":"$L3"}]],"_example_consumer/_public":"$L4"},"children":["$","$L5","_example_consumer/_public",{"id":"_example_consumer/_public"}]}]
6:I["rsc/remote/client/_example_consumer:./__/__/framework/dist/framework.client.js#StreamRea
@jacob-ebey
jacob-ebey / routes.test.ts
Last active December 4, 2023 09:39
Assemble react-router routes from Vite `import.meta.glob`
import { describe, it, expect } from "vitest";
import { routesFromGlob, type RoutesFromGlob } from "./routes";
describe("routesFromGlob", () => {
it("correctly transforms routes", () => {
let routes = {
"./routes/_index/route.tsx": () => "Index Route",
"./routes/single/route.tsx": () => "Single Route",
"./routes/$param/route.tsx": () => "Param Route",
@jacob-ebey
jacob-ebey / example.tsx
Last active December 4, 2023 09:13
vite-react-router-remix-glob-routes
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import { globRoutes } from "@/lib/routes";
const router = createBrowserRouter(
globRoutes(import.meta.glob("./routes/**/route.tsx"))
);
function App() {
return <RouterProvider router={router} />;
@jacob-ebey
jacob-ebey / toggle.jsx
Created October 27, 2023 16:05 — forked from AndrewIngram/toggle.jsx
Naive sketch of mutation API
function getIsLiked(viewerId, entityId) {
// hit a database or something
}
function toggleLikeStatus(viewerId, entityId) {
// hit a database or something
return !currentStatus;
}
@jacob-ebey
jacob-ebey / server.js
Last active October 6, 2023 21:14
remix cjs server
const fs = require("node:fs");
const { createRequestHandler } = require("@remix-run/express");
const { broadcastDevReady, installGlobals } = require("@remix-run/node");
const compression = require("compression");
const express = require("express");
const morgan = require("morgan");
const sourceMapSupport = require("source-map-support");
sourceMapSupport.install();
@jacob-ebey
jacob-ebey / event-stream-reload-middleware.ts
Created September 28, 2023 06:51
Event Stream Dev Middleware
export function createDevMiddleware(pathname: string) {
return {
hmr(request: Request) {
return new Response(
new ReadableStream({
start(controller) {
controller.enqueue(
`id:0\nevent: message\ndata: ${JSON.stringify({
type: "connected",
})}\n\n`
@jacob-ebey
jacob-ebey / event-delegation.ts
Last active September 28, 2023 06:53
Remix Event Delegation
import * as React from "react";
import { useNavigate, useSubmit } from "@remix-run/react";
export function useEventDelegation() {
const navigate = useNavigate();
const submit = useSubmit();
React.useEffect(() => {
const handleClick = (e: MouseEvent) => {
const target = e.target as HTMLElement;
if (
@jacob-ebey
jacob-ebey / loader.ts
Last active September 21, 2023 17:31
Node RSC on-demand transform / resolution for RSC
import { readFile } from 'node:fs/promises'
import * as path from 'node:path'
import { type LoadHook, type ResolveHook, type ResolveHookContext } from 'node:module'
import { fileURLToPath } from 'node:url'
import * as oxy from '@oxidation-compiler/napi'
import type { ModuleExport } from './module-info.js'
import * as clientTransforms from './transform-client.js'