惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
S
Schneier on Security
The Hacker News
The Hacker News
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
I
Intezer
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Schneier on Security
Schneier on Security
Security Latest
Security Latest
AWS News Blog
AWS News Blog
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
有赞技术团队
有赞技术团队
博客园 - 叶小钗
The Last Watchdog
The Last Watchdog
O
OpenAI News
月光博客
月光博客
Hacker News: Ask HN
Hacker News: Ask HN
阮一峰的网络日志
阮一峰的网络日志
S
Security @ Cisco Blogs
Google Online Security Blog
Google Online Security Blog
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Latest news
Latest news
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Apple Machine Learning Research
Apple Machine Learning Research
U
Unit 42
PCI Perspectives
PCI Perspectives
博客园 - 聂微东
SecWiki News
SecWiki News
宝玉的分享
宝玉的分享
Forbes - Security
Forbes - Security
H
Heimdal Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Troy Hunt's Blog
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
罗磊的独立博客
WordPress大学
WordPress大学
D
Darknet – Hacking Tools, Hacker News & Cyber Security

Kent C. Dodds Blog

Implementing Hybrid Semantic + Lexical Search Simplifying Containers with Cloudflare Sandboxes Migrating to Workspaces and Nx Offloading FFmpeg with Cloudflare Building Semantic Search on my Content Helping YOU ask ME questions with AI How I used Cursor to Migrate Frameworks The Dow's Start on the Covenant Path 2025 in Review The next chapter: EpicAI.pro AI is taking your job How I increased my visibility Launching Epic Web 2023 in Review Stop Being a Junior RSC with Dan Abramov and Joe Savona Live Stream Fixing a Memory Leak in a Production Node.js App 2022 in Review My Car Accident I Migrated from a Postgres Cluster to Distributed SQLite with LiteFS I'm building EpicWeb.dev A review of my time at Remix Remix: The Yang to React's Yin How I help you build better websites Why I Love Remix The State Initializer Pattern How to React ⚛️ Get a catch block error message with TypeScript Building an awesome image loading experience How Remix makes CSS clashes predictable Introducing the new kentcdodds.com How I built a modern website in 2021 How to use React Context effectively Static vs Unit vs Integration vs E2E Testing for Frontend Apps The Testing Trophy and Testing Classifications Array reduce vs chaining vs for loop Don't Solve Problems, Eliminate Them Super Simple Start to Remix Super Simple Start to ESModules in Node.js JavaScript Pass By Value Function Parameters How to write a Constrained Identity Function (CIF) in TypeScript How to optimize your context value How to write a React Component in TypeScript TypeScript Function Syntaxes Listify a JavaScript Array Build vs Buy: Component Libraries edition Using fetch with TypeScript Wrapping React.useState with TypeScript Define function overload types with TypeScript 2020 in Review Business and Engineering alignment Hi, thanks for reaching out to me 👋 useEffect vs useLayoutEffect Super simple start to Firebase functions Super simple start to Netlify functions Super Simple Start to css variables Favor Progress Over Pride in Open Source Testing Implementation Details How getting into Open Source has been awesome for me useState lazy initialization and function updates Use ternaries rather than && in JSX Application State Management with React Use react-error-boundary to handle errors in React JavaScript to Know for React How I structure Express apps What open source project should I contribute to? When I follow TDD AHA Programming 💡 How I Record Educational Videos Should I write a test or fix a bug? Stop mocking fetch Intentional Career Building Improve test error messages of your abstractions Tracing user interactions with React Eliminate an entire category of bugs with a few simple tools Common mistakes with React Testing Library Super Simple Start to React Stop using client-side route redirects The State Reducer Pattern with React Hooks Function forms Replace axios with a simple custom fetch wrapper How to test custom React hooks React Production Performance Monitoring Should I useState or useReducer? Stop using isLoading booleans Make Your Test Fail Make your own DevTools An Argument for Automation Fix the "not wrapped in act(...)" warning Super Simple Start to ESModules in the Browser Implementing a simple state machine library in JavaScript 2010s Decade in Review Why users care about how you write code Why I avoid nesting closures Don't call a React function component Why your team needs TestingJavaScript.com Inversion of Control Understanding React's key prop How to Enable React Concurrent Mode Profile a React App for Performance
React/JSX as a server-side templating language
2018-10-01 · via Kent C. Dodds Blog

Last week at PayPal, one of my pull requests was merged in an express codebase which migrated us from a custom template system to using React function components and JSX. The motivation was to reduce the maintenance overhead of knowing and maintaining a custom template system in addition to the JSX we are doing on the frontend.

The app is paypal.me. The way it works is we have the home, terms, and supported countries pages that are 100% rendered HTML/CSS (and just a tiny bit of vanilla JS), and then the profile and settings pages are rendered by the server as "skeleton" html pages (with SEO-relevant tags and a root <div> etc.) and then the client-side React app kicks in to load the rest of the data/interactivity needed on the page.

You should use Remix, not just the skeleton index.html as we're doing on paypal.me. We have our reasons (there's nuance in everything and I'm not going to get into this). Of course, Remix didn't exist at the time we were working on this so 🤷‍♂️

Before my PR, we actually had two systems in place. We used express-es6-template-engine for the profile and settings pages (which are actually the same page), and for the marketing pages one of our engineers came up with a tagged-template literal solution that was react-like (with functions that accepted props and returned a string of HTML). So engineers that work on this codebase would have to know and maintain:

  1. express-es6-template-engine for the profile and settings pages
  2. React and JSX for the client-side app
  3. The custom tagged-template literal solution for the marketing pages.

It was decided to simplify this down to a single solution: React and JSX for both frontend and backend. And that's the task I took. I want to explain a few of the gotchas and solutions that I ran into while making this transition.

JSX compilation

This was actually as easy as npm install --save react react-dom in the server. Because paypal.me uses paypal-scripts, the server's already compiled with the built-in babel configuration which will automatically add the necessary react plugins if the project lists react as a dep. Nice! I LOVE Toolkits!

HTML Structure

The biggest challenge I faced with this involves integration with other PayPal modules that generate HTML that need to be inserted into the HTML that we're rendering. One such example of this is our polyfill service that I wrote about a while backwhich inserts a script tag that has some special query params and a server nonce. We have this as middleware and it adds a res.locals.polyfill.headHTML which is a string of HTML that needs to appear in the <head> that you render.

With the template literal and es6-template-engine thing we had, this was pretty simple. Just add ${polyfill.headHTML} in the right place and you're set. In React though, that's kinda tricky. Let's try it out. Let's assume that polyfill.headHTML is <script src="hello.js"></script>. So if we do this:

<head>{polyfill.headHTML}</head>

This will result in HTML that looks like this:

<head>&lt;script src=&quot;hello.js&quot;&gt;&lt;/script&gt;</head>

This is because React escapes rendered interpolated values (those which appear between { and }). This is a cross site-scripting (XSS) protection feature built-into React. All of our apps are safer because React does this. However, there are situations where it causes problems (like this one). So React gives you an escape hatch where you can opt-out of this protection. Let's use that:

<head>
	<div dangerouslySetInnerHTML={{ __html: polyfill.headHTML }} />
</head>

So this would result in:

<head>
	<div>
		<script src="hello.js" />
	</div>
</head>

But that's not at all semantically accurate. A div should not appear in a head. We also have some meta tags. It technically works in Chrome, but I don't know what would happen in all the browsers PayPal supports and I don't want to bust SEO or functionality of older, less-forgiving browsers for this.

So here's the solution I came up with that I don't hate:

<head>
	<RawText>{polyfill.headHTML}</RawText>
</head>

The implementation of that RawText component is pretty simple:

function RawText({ children }) {
	return <raw-text dangerouslySetInnerHTML={{ __html: children }} />
}

So this will result in:

<head>
	<raw-text>
		<script src="hello.js" />
	</raw-text>
</head>

This doesn't solve the problem by itself. Here's what we do when we render the page to HTML:

const htmlOutput = ReactDOMServer.renderToStaticMarkup(<Page {...options} />)
const rendered = `  
  <!DOCTYPE html>  
  ${removeRawText(htmlOutput)}  
`
// ...etc...

That removeRawText function is defined right next to the RawText component and looks like this:

function removeRawText(string) {
	return string.replace(/<\/?raw-text>/g, '')
}

So, effectively what our rendered string looks like is this:

<head>
	<script src="hello.js"></script>
</head>

🎉 Cool right?

So we have a simple component we can use for any raw string we want inserted as-is into the document without having to add an extra meaningless (and sometimes semantically harmful) DOM node in the mix. (Note, the real solution to this problem would be for React to support dangerouslySetInnerHTML on Fragments).

NOTE: The fact that this logic lives in a function right next to the definition of the RawText component rather than just hard-coding the replacement where it happens is IMPORTANT. Anyone coming to the codebase and seeing RawText or removeRawText will be able to find out what's going on much more quickly.

Localization

In our client-side app, we use a localization module that my friend Jamund and I worked on that relies on a singleton "store" of content strings. It works great because there's only one locale that'll ever be needed through the lifetime of the client-side application. Singletons don't work very well on the backend though. So I built a simple React Context consumer and provider which made it easier to get messages using this same abstraction without the singleton. I'm not going to share the code for it, but here's how you can use it:

<Message msgKey="marketing_pages/new_landing.title" />

It worked out pretty well. The Message component renders the MessageConsumer component which will get the content out of context and retrieve the message with the given key.

Other things of note:

  • React.Fragments are everywhere. When the structure matters so much, you find yourself using React fragments all over the place. We're using babel 7 and loving the new shorter syntax of <> and </>.
  • style/className changes. Before this was straightup HTML, the biggest changes I had to make was all the class="had to be changed to className=" which wasn't all that challenging, but I found myself forgetting the style="attributes needing to be changed to style={ and object syntax all the time. Luckily React gives you a warning if you miss one :)
  • ${ needed to be changed to {. I found a few stray $ rendered several times in the course of this refactor 😅

Conclusion

I'm pretty pleased that we now only have one templating solution for the entire app (both frontend and backend). I think that'll reduce the maintenance burden of the app and that's a real win. Trying things out and doing experiments is a good thing, but circling back to refactor things to the winning abstraction is an important step to making applications that are maintainable for the long-term. I hope this is helpful to you! Good luck!