Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to import from the remix bundle? #10

Open
tadeaspetak opened this issue Nov 10, 2023 · 1 comment
Open

Is it possible to import from the remix bundle? #10

tadeaspetak opened this issue Nov 10, 2023 · 1 comment

Comments

@tadeaspetak
Copy link

Hi there! First of all, thanks for this awesome repo, it showed me how to get started with a server file in TS which is exactly what I needed 🙏

I need to create a loader context and fetch a few things, such as the locale and user for that context. This is simple via the getLoadContext function.

But, in order to resolve locale and user, I need functions which I have available in the bundle built by the remix build command (which end up in build/index.js). Any idea as to the best way how to go about this? When I try to import them from the server.ts file built with esbuild, they are not automatically bundled in and naturally cannot be resolved from the built file at runtime.

@xHomu
Copy link
Owner

xHomu commented Nov 12, 2023

Hard to tell without seeing the code, but one potential route is pass it down via the server context, see https://remix.run/docs/en/main/route/loader#context

Take a look at how we does it on the Repay template:

  app.all(
    "*",
    createRequestHandler({
      // @ts-ignore
      build,
      getLoadContext(req, res) {
        return {
          payload: req.payload,
          user: req?.user,
          res,
        };
      },
    })
  );

https://github.com/manawiki/repay/blob/main/server.ts#L73

Which let us consume it here:

export const loader = async ({ context: { payload } }: LoaderFunctionArgs) => {
  const users = await payload.find({
    collection: "users",
  });

  return json({ userCount: users.totalDocs }, { status: 200 });
};

https://github.com/manawiki/repay/blob/main/app/routes/_index.tsx#L5

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

No branches or pull requests

2 participants