@greenweb/gaw-plugin-netlify-edge
v0.1.0
Published
This plugin provides some useful functions that can be used when setting up the Grid-aware Websites library using Netlify Edge Functions.
Downloads
81
Readme
Grid-aware Websites - Netlify Edge Plugin
This plugin provides some useful functions that can be used when setting up the @greenweb/grid-aware websites
library using Netlify Edge Functions.
After you have installed the @greenweb/grid-aware-websites
package (see steps), you can use this plugin to:
- Fetch the location of a user based on
geo
data that is exposed by the Netlify Edge platform.
Fetching location (getLocation()
)
The code below is a simplified demonstation of how to use this plugin to fetch the request location, and then use it with the gridAwarePower
function.
The worker code below will return the grid data back to the browser in JSON format.
import { gridAwarePower } from "@greenweb/grid-aware-websites";
import { getLocation } from "@greenweb/gaw-plugin-netlify-edge";
export default {
async fetch(request, env, ctx) {
const location = netlify.getLocation(request);
const { country } = location;
const gridData = await gridAwarePower(country, "API_KEY");
return new Response(gridData);
},
};
By default, the getLocation()
function returns the geo.country.code
value. However, it can also be used to return the geo.latitude
and geo.longitude
values if desired.
import { gridAwarePower } from "@greenweb/grid-aware-websites";
import { netlify } from "@greenweb/gaw-plugin-netlify-edge";
export default {
async fetch(request, env, ctx) {
const location = netlify.getLocation(request, {
mode: "latlon",
});
const { lat, lon } = location;
// Functionality yet to be built into grid-aware-websites library.
},
};
[!NOTE] Using latitude and longitude values is not yet supported in the
@greenweb/grid-aware-websites
package.