Nitro abstracts away a lot of platform functionality like key-value storage, route caching, and more. But not all of it! There's more useful information available to us in the Netlify function context.
Netlify Functions and Edge Functions expose a runtime context with geolocation, client IP, cookies, site metadata, and more. You can read this context from Nuxt server routes to personalize responses, implement regional logic, and more.
In an api endpoint, or on the server side on a page/component, you can access the context on the Netlify
global.
const netlifyContext = (globalThis as any)?.Netlify?.context ?? null;
The resulting netlifyContext
object will have all kinds of useful information. Here's an example of that context object:
{
"cookies": {},
"deploy": {
"id": "a1b2c3d4e5f6g7h8i9j0k1l2",
"published": false
},
"geo": {
"city": "Tuscaloosa",
"country": {
"code": "US",
"name": "United States"
},
"subdivision": {
"code": "AL",
"name": "Alabama"
},
"timezone": "America/Chicago",
"latitude": 0.0,
"longitude": 0.0,
"postalCode": "00000"
},
"ip": "0.0.0.0",
"params": {
"0": "api/geo"
},
"requestId": "01K237BX82FXV254MXMZPCH0BB",
"spanID": "",
"site": {
"id": "00000000-0000-0000-0000-000000000000",
"name": "test",
"url": "<http://test.netlify.app>"
},
"account": {
"id": "a1b2c3d4e5f6g7h8i9j0k1l2"
},
"server": {
"region": "aws-us-east-1"
},
"url": "<https://regal-cactus-1b3289.netlify.app/api/geo>"
}
You can view the full Netlify Edge Function Context
shape in the docs
This context is available in both regular functions and edge function contexts. If you want run the Nuxt server in an edge function, set the following environment variable in the Netlify dashboard:
# Netlify UI → Site settings → Environment variables
SERVER_PRESET=netlify_edge
Reference: Nuxt’s Netlify deployment guide (Nuxt → Netlify).
Now that you know the "how", let's look at some practical use cases for Netlify Context in Nuxt.
context.geo.country.code
to select copy, currency, or shipping options.geo
with care for caching.context.ip
for basic rate limiting or blocklists before hitting origin services.Netlify-Vary
to keep cache cardinality under control. See advanced Nuxt on Netlify caching guidance (guide).routeRules
).There are certainly some exciting opportunities when you combine the Netlify Context with the power of Nuxt.
This approach is not without its caveats. If implementing, do consider the following:
Cache-Control: no-store
or use Netlify-Vary
with a small, controlled set of keys to capture the right info for the right user.Netlify.context
is null locally. This is expected. You'll need to verify on a deployed Netlify site.Do you have any other use cases for Netlify Context in Nuxt? Let us know in the comments! If you'd like to learn more about Nuxt, checkout the complete video course Mastering Nuxt.
Our goal is to be the number one source of Vue.js knowledge for all skill levels. We offer the knowledge of our industry leaders through awesome video courses for a ridiculously low price.
More than 200.000 users have already joined us. You are welcome too!
© All rights reserved. Made with ❤️ by BitterBrains, Inc.