首页 web前端 js教程 从零到发布:我们 Remix 驱动的开发之旅的主要收获

从零到发布:我们 Remix 驱动的开发之旅的主要收获

Sep 07, 2024 am 12:01 AM

大约六个月前,我做出了一些人所说的大胆决定,选择 Remix 作为我们公司 Web 应用程序的基础。快进到今天,我认为是时候退一步反思我们所做的选择了。我将回顾所做的主要基础设施决策,并附带一些实际使用示例。

所以,事不宜迟,让我们直接进入这段旅程的亮点和低谷——满足感和经验教训的结合。

From Zero to Launch: Key Takeaways from Our Remix-Powered Development Journey

Remix(或者我应该说 React Router?)

亮点:混音

这可能是我当时做出的“风险最大”的基础设施决策,因为 Remix 远没有 NextJS 那么受欢迎,而且据我所知,大企业使用 Remix 的例子并不多。
快进到今天 - ChatGPT 几天前从 Next 迁移到 Remix!

正如我在上一篇文章中详细介绍的那样,我选择 Remix 的原因有很多,其中一些原因是它的简单性、“全栈”方面(即,利用 remix 服务器作为“前端的后端”)以及它对路由、数据获取和突变。

幸运的是,Remix 交付了?该框架直观、易于学习和教导他人,并确保使用最佳实践,使编写代码和测试变得简单。

与 Remix 合作几个月后,他们宣布与 React Router 正式合并,我希望这能说服更多的人使用它,就像他们转向 vite 所做的那样。

在很多情况下我都清楚地意识到 Remix 是正确的选择。我将给出一个我最近处理的实际示例 - 在混音服务器中使用单个记录器实例能够记录和跟踪整个应用程序中的操作和错误,以增强我们的监控能力。实施非常简单:

第 1 步 - 创建记录器(在我的例子中,我使用了 Winston,它与我们用于监控的 Datadog 配合得很好)

第 2 步 - 将记录器添加到服务器的加载上下文(在我的例子中是express):

app.all(
  '*',
  createRequestHandler({
    getLoadContext: () => ({
      logger,
      // add any other context variables here
    }),
    mode: MODE,
    // ...
  }),
);

第 3 步(适用于 Typescript 用户)- 更新 Remix 的默认类型定义以将记录器包含在应用程序加载上下文中

import '@remix-run/node';
import { type Logger } from 'winston';

declare module '@remix-run/node' {
  interface AppLoadContext {
    logger: Logger;
  }
}

第 4 步 - 在任何路线的加载器或操作中随意使用记录器!

export async function action({ request, context }: ActionFunctionArgs) {

  try {
    await someAction();
  } catch (e) {
    context.logger.error(e);
  }
}

在我们结束本节之前,我想说的是,还有一些我希望 Remix 拥有但它们还没有的东西,比如用于流数据/组件的 RSC 实现,以及非常适合身份验证的路由中间件/授权。幸运的是,看起来这些东西(以及其他很酷的功能)在他们的路线图中被优先考虑,所以希望我们能尽快得到它们!

From Zero to Launch: Key Takeaways from Our Remix-Powered Development Journey

Tanstack 查询。一直以来的最爱

亮点:反应查询

根据我过去的积极经验,选择 @tanstack/react-query 对我来说是一个简单的决定,这次也没有让人失望。该 API 是通用的、可扩展的,并且以最佳方式不拘一格 — 使其易于与其他工具集成。

我非常喜欢它,所以我选择了它,因为我知道我们的内部 API 是基于 GraphQL 的,而不是更明显的选择 Apollo Client。原因有很多:Tanstack Query 拥有出色的 API,它比 Apollo 轻得多,并且因为我不想依赖像 GraphQL 这样专门针对特定技术量身定制的工具,以防万一我们需要切换或合并其他技术。

此外,由于我们使用 Remix,我可以充分利用 Tanstack Query 的 SSR 功能 - 在服务器端预取查询,同时仍然保持在客户端变异、无效或重新获取这些查询的能力。这是一个简化的示例:

import { dehydrate, QueryClient, HydrationBoundary, useQuery } from '@tanstack/react-query';
import { json, useLoaderData } from '@remix-run/react';


const someDataQuery = {
  queryKey: ['some-data'],
  queryFn: () => fetchSomeData()
}

export async function loader() {
  const queryClient = new QueryClient();
  try {
    await queryClient.fetchQuery(someDataQuery);

    return json({ dehydrate: dehydrate(queryClient) });
  } catch (e) {
    // decide whether to handle the error or continue to
    // render the page and retry the query in the client
  }
}

export default function MyRouteComponent() {
  const { dehydratedState } = useLoaderData<typeof loader>();
  const { data } = useQuery(someDataQuery);

  return (
           <HydrationBoundary state={dehydratedState}>
             <SomeComponent data={data} />
           </HydrationBoundary />
  );
}

From Zero to Launch: Key Takeaways from Our Remix-Powered Development Journey

Tailwind CSS

Highlight: Tailwind

I was initially skeptical about Tailwind, having never used it before, and because I didn’t quite understand the hype (it seemed to me at first just like syntactic sugar over CSS). However, I decided to give it a try because of its strong recommendations and popularity within the community, and I’m really glad I did. Tailwind’s utility-first approach made it incredibly easy to build a consistent and robust design system right from the start, which, looking back, was a total game changer.
It also pairs perfectly with shadcn, which we used, and together they allowed me to deliver quickly while keeping everything modular and easy to modify later on - a crucial advantage in a startup environment.

I also really like how easy it is to customize tailwind's theme to your needs - for example, overriding tailwind's default scheme:

First, define your colors as variable's under tailwind's main .css file:

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {

  :root {
    /* define the primitive design system tokens */
    --colors-blue-100: hsl(188 76% 90%);
    --colors-blue-200: hsl(187 63% 82%);
    --colors-blue-25: hsl(185 100% 98%);
    --colors-blue-300: hsl(190 52% 74%);
    --colors-blue-400: hsl(190 52% 61%);
    --colors-blue-50: hsl(188 92% 95%);
    --colors-blue-500: hsl(190 74% 39%);
    --colors-blue-600: hsl(191 77% 34%);
    --colors-blue-700: hsl(190 51% 35%);
    --colors-blue-800: hsl(191 52% 29%);
    --colors-blue-900: hsl(190 51% 23%);
    --colors-blue-950: hsl(190 52% 17%);
    --colors-gray-100: hsl(0 0 90%);
    --colors-gray-200: hsl(0 0 85%);
    --colors-gray-25: hsl(0 0 98%);
    --colors-gray-300: hsl(0 0 73%);
    --colors-gray-400: hsl(0 1% 62%);
    --colors-gray-50: hsl(0 0 94%);
    --colors-gray-500: hsl(0 0% 53%);
    --colors-gray-600: hsl(0 0 44%);
    --colors-gray-700: hsl(0 0 36%);
    --colors-gray-800: hsl(0 2% 28%);
    --colors-gray-900: hsl(0 0 20%);
    --colors-gray-950: hsl(0 0 5%);
    --colors-red-100: hsl(4 93% 94%);
    --colors-red-200: hsl(3 96% 89%);
    --colors-red-25: hsl(12 100% 99%);
    --colors-red-300: hsl(4 96% 80%);
    --colors-red-400: hsl(4 92% 69%);
    --colors-red-50: hsl(5 86% 97%);
    --colors-red-500: hsl(4 88% 61%);
    --colors-red-600: hsl(4 74% 49%);
    --colors-red-700: hsl(4 76% 40%);
    --colors-red-800: hsl(4 72% 33%);
    --colors-red-900: hsl(8 65% 29%);
    --colors-red-950: hsl(8 75% 19%);

    /*
      ...
    */

    /* define the semantic design system tokens */

    --primary-light: var(--colors-blue-200);
    --primary: var(--colors-blue-600);
    --primary-dark: var(--colors-blue-800);
    --primary-hover: var(--colors-blue-50);

    --text-default-primary: var(--colors-gray-700);
    --text-default-secondary: var(--colors-gray-800);
    --text-default-tertiary: var(--colors-gray-900);
    --text-default-disabled: var(--colors-gray-300);
    --text-default-read-only: var(--colors-gray-400);

    --disabled: var(--colors-gray-300);
    --tertiary: var(--colors-gray-50);

    /*
      ...
    */
  }
}

Then, extend Tailwind's default theme via the tailwind config file:

import { type Config } from 'tailwindcss';

const ColorTokens = {
  BLUE: 'blue',
  GRAY: 'gray',
  RED: 'red',
} as const;

const generateColorScale = (colorName: string) => {
  const scales = [25, 50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950];
  return scales.reduce(
    (acc, scale) => {
      acc[scale] = `var(--colors-${colorName}-${scale})`;
      return acc;
    },
    {} as Record<string, string>,
  );
};

export const customColors = Object.values(ColorTokens).reduce((acc, color) => {
  return {
    ...acc,
    [color]: generateColorScale(color),
  };
}, {});

const config = {
  // ... additional config
  theme: {
    extend: {
      colors: customColors
    },
  },
} satisfies Config;

export default config;

This is just the tip of the iceberg - you can go on to define custom spacing, text sizing and much more!

From Zero to Launch: Key Takeaways from Our Remix-Powered Development Journey

Playwright - makes writing e2e tests fun

Highlight: Playwright

Previously using Cypress, I was inclined to choose it, but I kept hearing hype around Playwright and figured I'll research it extensively before making a decision. After comparing Playwright with Cypress, it was clear Playwright is the right choice to make - the fact it comes with parallel execution out of the box, the broader browser support, running times and debugging capabilities - all made Playwright the obvious choice.
And, while this is very subjective, I like Playwright's syntax much better. I find it similar to React Testing Library's syntax, which I like, and I tend to think the tests are a lot more readable, with the asynchronous aspect of the tests being very straight forward, unlike the syntax of Cypress that can cause tests to feel bloated by .then() statements and subsequent indentations.

I think my favorite feature of Playwright is their implementation of Test Fixtures. They provide a clean way to initialize and reuse resources like page objects, making tests more modular and maintainable. Make sure to check out the above link to learn more about it!

From Zero to Launch: Key Takeaways from Our Remix-Powered Development Journey

Tanstack Table vs AG Grid

Lowlight: (Starting with) Tanstack Table

First off, let me clarify — @tanstack/react-table is a fantastic tool, which is why I was inclined to choose it in the first place, but it wasn’t the best fit for my particular use case. The very features that make it great, like its small bundle size and customizable API, ended up being less relevant to our needs than I originally thought. Despite having full control of the rendering of the Table, I was having some issues aligning its scrolling behavior to our desired outcome (why is it still not possible in 2024 to have a

element with dynamic sizing and scrolling on its body only, without resorting to clunky solutions? ?).

I soon realized that to deliver my feature fast and provide a good user experience, I needed a table with built-in features like pagination, column resizing and row auto-sizing, and I preferred having those out of the box over full control of the UI rendering. Additionally, since the table only appears after a query is run, I could lazy load it, making the bundle size less of a concern.

I highly recommend using the AG Grid theme builder to customize AG Grid according to your preferences/design system. And, for those using Cypress for their testing purposes - I found this cool plugin that abstracts AG Grid to easily interact with it in tests (sadly I could not find the same for Playwright ?)

Final thoughts

Looking back, I definitely feel a sense of pride in what we’ve accomplished. Not every decision was perfect, but taking the time to research and find the most fitting solution was worth it. And when things didn’t go as planned - it challenged us to think critically and adapt quickly, which is important no less.

Please let me know in the comments if there’s something you’d like to see explored further in future articles.
Here’s to more lessons learned, personal growth and having fun along the way ?

以上是从零到发布:我们 Remix 驱动的开发之旅的主要收获的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Stock Market GPT

Stock Market GPT

人工智能驱动投资研究,做出更明智的决策

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

JavaScript实现点击图片切换效果:专业教程 JavaScript实现点击图片切换效果:专业教程 Sep 18, 2025 pm 01:03 PM

本文将介绍如何使用JavaScript实现点击图片切换的效果。核心思路是利用HTML5的data-*属性存储备用图片路径,并通过JavaScript监听点击事件,动态切换src属性,从而实现图片切换。本文将提供详细的代码示例和解释,帮助你理解和掌握这种常用的交互效果。

如何使用JavaScript中的GeOlocation API获取用户的位置? 如何使用JavaScript中的GeOlocation API获取用户的位置? Sep 21, 2025 am 06:19 AM

首先检查浏览器是否支持GeolocationAPI,若支持则调用getCurrentPosition()获取用户当前位置坐标,并通过成功回调获取纬度和经度值,同时提供错误回调处理权限被拒、位置不可用或超时等异常,还可传入配置选项以启用高精度、设置超时时间和缓存有效期,整个过程需用户授权并做好相应错误处理。

如何在JavaScript中使用setInterval创建重复间隔 如何在JavaScript中使用setInterval创建重复间隔 Sep 21, 2025 am 05:31 AM

要创建JavaScript中的重复间隔,需使用setInterval()函数,它会以指定毫秒数为间隔重复执行函数或代码块,例如setInterval(()=>{console.log("每2秒执行一次");},2000)会每隔2秒输出一次消息,直到通过clearInterval(intervalId)清除,实际应用中可用于更新时钟、轮询服务器等场景,但需注意最小延迟限制、函数执行时间影响,并在不再需要时及时清除间隔以避免内存泄漏,特别是在组件卸载或页面关闭前应清理,确保

NUXT 3组成API解释了 NUXT 3组成API解释了 Sep 20, 2025 am 03:00 AM

Nuxt3的CompositionAPI核心用法包括:1.definePageMeta用于定义页面元信息,如标题、布局和中间件,需在中直接调用,不可置于条件语句中;2.useHead用于管理页面头部标签,支持静态和响应式更新,需与definePageMeta配合实现SEO优化;3.useAsyncData用于安全地获取异步数据,自动处理loading和error状态,支持服务端和客户端数据获取控制;4.useFetch是useAsyncData与$fetch的封装,自动推断请求key,避免重复请

JavaScript中DOM元素访问的常见陷阱与解决方案 JavaScript中DOM元素访问的常见陷阱与解决方案 Sep 15, 2025 pm 01:24 PM

本文旨在解决JavaScript中通过document.getElementById()获取DOM元素时返回null的问题。核心在于理解脚本执行时机与DOM解析状态。通过正确放置标签或利用DOMContentLoaded事件,可以确保在元素可用时再尝试访问,从而有效避免此类错误。

如何将文本复制到JavaScript中的剪贴板? 如何将文本复制到JavaScript中的剪贴板? Sep 18, 2025 am 03:50 AM

使用ClipboardAPI的writeText方法可复制文本到剪贴板,需在安全上下文和用户交互中调用,支持现代浏览器,旧版可用execCommand降级处理。

JavaScript中数字格式化:使用toFixed()方法保留固定小数位 JavaScript中数字格式化:使用toFixed()方法保留固定小数位 Sep 16, 2025 am 11:57 AM

本教程详细讲解如何在JavaScript中将数字格式化为固定两位小数的字符串,即使是整数也能显示为"#.00"的形式。我们将重点介绍Number.prototype.toFixed()方法的使用,包括其语法、功能、示例代码以及需要注意的关键点,如其返回类型始终为字符串。

如何在JavaScript中创建多行字符串? 如何在JavaScript中创建多行字符串? Sep 20, 2025 am 06:11 AM

thebestatoreateamulti-linestlinginjavascriptsisisingsistisingtemplatalalswithbacktticks,whatpreserveticks,whatpreservereakeandeexactlyaswrite。

See all articles