Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

remix-env

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package version was removed
This package version has been unpublished, mostly likely due to security reasons

remix-env

Easy way to use process.env in your Remix apps

unpublished
Source
npmnpm
Version
2.0.2
Version published
Weekly downloads
23
-17.86%
Maintainers
1
Weekly downloads
 
Created
Source

remix-env

Easy way to use process.env in your Remix apps

Setup

app/entry.server.tsx

  • Run npm install remix-env or yarn add remix-env
  • Add setupEnv in remix.config.js
  • Using injectEnv to markup in entry.server.tsx

remix.config.js

+ const { setUpEnv } = require('remix-env')

+ setUpEnv()
/**
 * @type {import('@remix-run/dev').AppConfig}
 */
module.exports = {
  // any configa
}

entry.server.tsx

import { renderToString } from "react-dom/server";
import type {
  EntryContext,
  HandleDataRequestFunction,
} from "@remix-run/node"; // or cloudflare/deno
import { RemixServer } from "@remix-run/react";
+ import { injectEnv } from 'remix-env'

export default function handleRequest(
  request: Request,
  responseStatusCode: number,
  responseHeaders: Headers,
  remixContext: EntryContext
) {
  const markup = renderToString(
    <RemixServer context={remixContext} url={request.url} />
  );

+   const markUpWithEnv = injectEnv(markup)

  responseHeaders.set("Content-Type", "text/html");

-  return new Response("<!DOCTYPE html>" + markUp, {
+  return new Response("<!DOCTYPE html>" + markUpWithEnv, {
    status: responseStatusCode,
    headers: responseHeaders,
  });
}

Usage

Now, You can use the getEnv to get your env

const env = getEnv()

// app/routes/index.tsx
export default function IndexRoute() {
  return (
    <div>
      {env.PUBLIC_ENV_FOO}
    </div>
  );
}

Configuration

By default this library will inject all environment variables with prefix PUBLIC_ENV_ to the browser.

But you can customize it by provide the filter function at createEnv.

const publicEnv = createEnv({ filterEnv: (key, value) => {
  // Only env that starts with PUBLIC_ENV_
  return key.startsWith("PUBLIC_ENV_");
})

Keywords

remix

FAQs

Package last updated on 02 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts