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

Add Vercel deploy config and platform specific tweaks #75

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/i18next.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import i18n from '~/i18n'; // your i18n configuration file
import HttpBackend from 'i18next-http-backend';
import {
IS_CF_PAGES,
IS_VERCEL,
safeRequireNodeDependency,
} from '~/utils/platform-adapter';
import { RemixI18NextOption } from 'remix-i18next/build/server';
Expand All @@ -13,6 +14,8 @@ import { findLanguageJSON } from '~/languages.server';
export async function getPlatformBackend() {
if (IS_CF_PAGES) {
return HttpBackend;
} else if (IS_VERCEL) {
return await import('i18next-fs-backend').then((module) => module.default);
} else {
return await safeRequireNodeDependency('i18next-fs-backend').then(
(module) => module.default,
Expand Down
2 changes: 2 additions & 0 deletions app/utils/platform-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const IS_CF_PAGES = typeof process === 'undefined';

export const IS_VERCEL = typeof process !== 'undefined' && process.env.VERCEL;

// This hack is to prevent `node` modules/packages being bundled in the
// Cloudflare Pages context, which causes an error.
export async function safeRequireNodeDependency(module: string) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"scripts": {
"build:cf": "cross-env CF_PAGES=1 remix build",
"build:nf": "cross-env NETLIFY=1 remix build",
"build:vl": "cross-env VERCEL=1 remix build",
"build": "remix build",
"dev:wrangler": "cross-env CF_PAGES=1 wrangler pages dev ./public",
"dev": "remix dev",
Expand Down
30 changes: 23 additions & 7 deletions remix.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@ import { createRoutesFromFolders } from '@remix-run/v1-route-convention';
/**
* @type {import('@remix-run/dev').AppConfig}
*/
const commonConfig = {
appDirectory: 'app',
serverModuleFormat: 'esm',
const bareConfig = {
serverDependenciesToBundle: [
'remix-i18next',
'@remix-validated-form/with-zod',
],
tailwind: true,
routes(defineRoutes) {
// uses the v1 convention, works in v1.15+ and v2
return createRoutesFromFolders(defineRoutes);
},
};

/**
* @type {import('@remix-run/dev').AppConfig}
*/
const commonConfig = {
appDirectory: 'app',
serverModuleFormat: 'esm',
tailwind: true,
...bareConfig,
};

/**
* @type {import('@remix-run/dev').AppConfig}
*/
Expand All @@ -28,6 +35,13 @@ const cloudflarePagesConfig = {
serverMinify: true,
...commonConfig,
};
/**
* @type {import('@remix-run/dev').AppConfig}
*/
const vercelConfig = {
ignoredRouteFiles: ['**/.*'],
...bareConfig,
};
/**
* @type {import('@remix-run/dev').AppConfig}
*/
Expand Down Expand Up @@ -61,11 +75,13 @@ const buildConfig = {
};

function selectConfig() {
if (!['development', 'production'].includes(process.env.NODE_ENV))
throw new Error(`Unknown NODE_ENV: ${process.env.NODE_ENV}`);
const ENV = process.env?.NODE_ENV || process.env?.VERCEL_ENV;
if (!['preview', 'development', 'production'].includes(ENV))
throw new Error(`Unknown ENV: ${ENV}`);
if (process.env.CF_PAGES) return cloudflarePagesConfig;
if (process.env.NETLIFY) return netlifyConfig;
if (process.env.NODE_ENV === 'development') return devConfig;
if (process.env.VERCEL) return vercelConfig;
if (ENV === 'development') return devConfig;
if (!process.env.CF_PAGES && !process.env.NETLIFY) return buildConfig;
throw new Error(`Cannot select config`);
}
Expand Down