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

推荐订阅源

Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
C
Cisco Blogs
Cloudbric
Cloudbric
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Security Archives - TechRepublic
Security Archives - TechRepublic
TaoSecurity Blog
TaoSecurity Blog
V2EX - 技术
V2EX - 技术
H
Heimdal Security Blog
S
Security Affairs
L
Lohrmann on Cybersecurity
Hacker News - Newest:
Hacker News - Newest: "LLM"
Simon Willison's Weblog
Simon Willison's Weblog
WordPress大学
WordPress大学
小众软件
小众软件
Security Latest
Security Latest
AWS News Blog
AWS News Blog
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
Engineering at Meta
Engineering at Meta
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
F
Full Disclosure
S
Schneier on Security
L
LangChain Blog
MyScale Blog
MyScale Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
Google Online Security Blog
Google Online Security Blog
Scott Helme
Scott Helme
Stack Overflow Blog
Stack Overflow Blog
爱范儿
爱范儿
A
Arctic Wolf
Martin Fowler
Martin Fowler
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
The Register - Security
The Register - Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
Latest news
Latest news
F
Fortinet All Blogs
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
Hacker News: Ask HN
Hacker News: Ask HN

The Nuxt Blog

Meet Nuxi · Nuxt Blog Introducing the Nuxt Agent · Nuxt Blog Nuxt 4.4 · Nuxt Blog Nuxt 4.3 · Nuxt Blog Building an MCP Server for Nuxt · Nuxt Blog Nuxt Image v2 · Nuxt Blog Nuxt 4.2 · Nuxt Blog Nuxt UI v4 · Nuxt Blog Nuxt 4.1 · Nuxt Blog Nuxt 3.18 · Nuxt Blog Announcing Nuxt 4.0 · Nuxt Blog Building a Privacy-First Feedback Widget · Nuxt Blog Roadmap to v4 · Nuxt Blog Nuxt 3.17 · Nuxt Blog Nuxt UI v3 · Nuxt Blog Nuxt 3.16 · Nuxt Blog Nuxt 3.15 · Nuxt Blog Introducing Nuxt Icon v1 · Nuxt Blog Nuxt 3.14 · Nuxt Blog Nuxt 3.13 · Nuxt Blog Introducing Nuxt Scripts · Nuxt Blog Nuxt 2 End-of-Life (EOL) · Nuxt Blog Nuxt 3.12 · Nuxt Blog Refreshed Nuxt ESLint Integrations · Nuxt Blog Nuxt: Looking forward · Nuxt Blog Nuxt 3.11 · Nuxt Blog The Evolution of Shiki v1.0 · Nuxt Blog Nuxt 3.10 · Nuxt Blog Nuxt 3.9 · Nuxt Blog Nuxt DevTools v1.0 · Nuxt Blog Nuxt 3.8 · Nuxt Blog A New Website · Nuxt Blog Nuxt 3.7 · Nuxt Blog Nuxt 3.6 · Nuxt Blog Nuxt 3.5 · Nuxt Blog Nuxt 3.4 · Nuxt Blog Introducing Nuxt DevTools · Nuxt Blog Nuxt 3.3 · Nuxt Blog Nuxt: A vision for 2023 · Nuxt Blog Announcing 3.0 · Nuxt Blog Announcing Nuxt 3 Release Candidate · Nuxt Blog Introducing Nuxt 3 Beta · Nuxt Blog Nuxt 2 Static Improvements · Nuxt Blog Going Full Static · Nuxt Blog Understanding how fetch works in Nuxt 2.12 · Nuxt Blog Nuxt 2: From Terminal to Browser · Nuxt Blog Introducing Smart Prefetching · Nuxt Blog Navigation · Nuxt Blog
Nuxt on the Edge · Nuxt Blog
2023-07-13 · via The Nuxt Blog

Introduction

In September 2017, Cloudflare introduced Cloudflare Workers, giving the ability to run JavaScript on their edge network. This means your code will deploy on the entire edge network in over a hundred locations worldwide in about 30 seconds. This technology allows you to focus on writing your application close to your users, wherever they are in the world (~50ms latency).

The worker's runtime is not the same as Node.js or the Browser, it executes the code using V8, the JavaScript engine developed by Google Chrome. Until now, what you could run on their platform were small scripts running on the edge before hitting your server to increase the performance or add some logic based on request headers, for example.

In November 2020, while working on Nuxt 3, we made the bet to run Nuxt in-production on edge runtimes / V8 isolates.

It unlocks the ability to server-render pages in ~50ms from all over the world when using a platform like CloudFlare Workers, without having to deal with servers, load balancers and caching, for about $0.3 per million requests. As of today, new platforms are coming to let run apps on V8 isolates such as Deno Deploy.

2024 update: I released NuxtHub to let you build full-stack applications with Nuxt on the edge, on your Cloudflare account with zero configuration. It includes a database, blob storage, KV, remote storage and more.

2026 update: NuxtHub is now multi-providers. It adds database, KV, blob storage, and cache to your Nuxt application and works on Vercel, Cloudflare, Netlify, Deno, and more. Learn more on hub.nuxt.com.

The Challenge

In order to make Nuxt run in workers, we had to rewrite some parts of Nuxt to be environmentally agnostic (runs in Node.js, Browser or V8).

We started with our server and created unjs/h3: a minimal http framework built for high performance and portability. It replaces Connect we used in Nuxt 2 but has compatibility with it so you can keep using Connect/Express middleware. In the workers, for each incoming request, it starts Nuxt in production, sends the request to it and sends back the response.

In Nuxt 2, the duration to start the server in production in memory (also named cold start) was about ~300ms, because we had to load all the dependencies of your server and application in order to handle the request.

By working on h3, we decided to code-split each handler attached to the server and lazy-load them only when requested. When you start Nuxt 3, we only load h3 in memory and the corresponding handlers. When a request comes in, we load the handler corresponding to the route and execute it.

By adopting this approach, we reduced the cold start from ~300ms to ~2ms.

We had another challenge in order to run Nuxt on the edge: the production bundle size. This includes the server, Vue app and Node.js dependencies combined. Cloudflare workers currently have a limit of 1MB (free plan) and 5MB ($5 per month plan) for the worker size.

In order to achieve this, we created unjs/nitro, our server engine, when running the nuxt build command, it bundles your whole project and includes all dependencies into the final output. It uses Rollup and vercel/nft to trace only the code used by the node_modules to remove unnecessary code. The total size of the generated output for a basic Nuxt 3 application is about 700kB gzip.

Lastly, to provide the same developer experience between development (Node.js) and production on Cloudflare (Edge runtime), we created unjs/unenv: a library to convert JavaScript code to run everywhere (platform agnostic) by mocking or adding polyfills for known dependencies.

At Nuxt, we believe that you should have the freedom to choose the hosting provider that fits you best.

This is why you can deploy a Nuxt application with edge-side rendering on:

We also support many other deployment providers, including static hosting or traditional Node.js serverless and server hosts.

Pushing Full-stack Capabilities

Now that we have Nuxt running on edge runtime, we can do more than render a Vue application. Thanks to the server directory, creating an API route is a TypeScript file away.

To add the /api/hello route, create a server/api/hello.ts file:

server/api/hello.ts

export default defineEventHandler((event) => {
  return {
    hello: 'world'
  }
})

You can now universally call this API in your pages and components:

pages/index.vue

<script setup>
const { data } = await useFetch('/api/hello')
</script>

<template>
  <pre>{{ data }}</pre>
</template>

One important thing to note when we created useFetch and $fetch is that during server-side rendering, if you call your API routes, it will emulate the request and call the function code directly: avoiding an HTTP request and reducing page’s rendering time.

In terms of developer experience, you will notice that when creating server files, the Nuxt server keeps running without rebuilding the Vue app. This is because Nuxt 3 supports Hot Module Replacement (HMR) when creating API and server routes.

Furthermore, by leveraging Object Relational Mapping (ORM) like drizzle-orm, developers can connect Edge & Serverless databases such as D1, Turso, Neon, Planetscale and more.

I created Atidone, an open source demo to showcase a full-stack application with authentication and a database running on the edge. The source code is available on GitHub under the MIT license at atinux/atidone.

Conclusion

We are excited about edge-side rendering and what it unlocks. Our team at Nuxt can’t wait to see what you will build on top of this!

Feel free to join our Discord server or mention @nuxt_js on Twitter to share your work.