Node.js Loader

πŸ‘¨β€πŸ’Ό Great job!
πŸ¦‰ So when Node.js imports the file, instead of this:
'use client'
import { createElement as h, useRef, useState } from 'react'
import { flushSync } from 'react-dom'

// ...

export function EditableText({ id, shipId, initialValue = '' }) {
	// ...
}
It's getting this:
import {registerClientReference} from "react-server-dom-esm/server";
export const EditableText = registerClientReference(function() {throw new Error("Attempted to call EditableText() from the server but EditableText is on the client. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a Client Component.");},"file:///Users/kentcdodds/code/epicweb-dev/react-server-components/playground/ui/edit-text.js","EditableText");
And when I console.log(EditableText.toString()) in , I'm getting this:
function() {throw new Error("Attempted to call EditableText() from the server but EditableText is on the client. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a Client Component.");}
Meaning if I try to call it in the RSC environment, I get an error.
(If you added the logs, you should have seen something like that in the console). That transformation is happening thanks to the loader you've registered.
The important elements here are:
  1. All exports of this module are wrapped in registerClientReference.
  2. The error message is thrown when the function is called on the server (because it should not be).
  3. The path to the file is passed as the second argument to registerClientReference.
  4. The name of the export is passed as the third argument to registerClientReference.
The registerClientReference function uses the path and name to generate a unique identifier for the client-side function. This identifier is used to generate a reference when rendering our server components that use these client components.
No build tool. Just built-in runtime features of Node.js. Cool huh!?
πŸ§β€β™‚οΈ I'm going to add some error boundaries in our app to get it ready for the work you're going to do. Check out my changes if you like.
Login to get access to the exclusive discord channel.
  • General
    Failed to fetch dynamically imported module...
    FacuPerezM:
    I am getting this error out of nowhere, in the 'test' tab inside the Workshop app. ``` Failed to f...
    1 Β· 9 hours ago
  • General
    Runninng Learning app on iPad?
    marcus_polonus πŸš€:
    Hi all, Did anyone managed to setup learning app on iPad? I tried to use codesandbox, but when I am...
    0 Β· a day ago
  • General
    Clicking an exercise file launch VIM instead of VS Code
    DBattou πŸš€:
    I started going through the workshops, everything is setup and working fine except for opening a fil...
    • βœ…1
    2 Β· 5 days ago
  • General
    Epicshop not saving the video resolution
    Toni πŸš€:
    Playback speed and caption settings are saved, but the video resolution always defaults to auto. Is ...
    • βœ…1
    7 Β· 2 days ago
  • General
    Launch editor error: spawn code ENOENT
    Andrew Elans 🌌 πŸš€:
    Clicking index.html gives this error in VS Code. Both from Chrome and Safari. Anyone?
    • βœ…2
    3 Β· 20 hours ago
  • General
    Welcome to EpicReact.dev! Say Hello πŸ‘‹
    Kent C. Dodds β—† πŸš€πŸ†πŸŒŒ:
    Welcome to the first of many posts in the EpicReact.dev channel! Take a moment to introduce yourself...
    • πŸš€5
    34 Β· 15 hours ago