Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ Read these recent blog posts focused on Enterprise releases, features, and use c

- [Netlify + AI: Why’d my deploy fail?](https://www.netlify.com/blog/netlify-ai-why-did-my-deploy-fail/)
- [Full control over caching with cache ID](https://www.netlify.com/blog/full-control-over-caching-with-cache-id/)
- [Introducing Netlify Image CDN Beta](https://www.netlify.com/blog/introducing-netlify-image-cdn-beta/)
- [[Introducing Netlify Image CDN](https://docs.netlify.com/image-cdn/overview/)]
- [Introducing Netlify Blobs Beta](https://www.netlify.com/blog/introducing-netlify-blobs-beta/)
- [Cache-tags & Purge API](https://www.netlify.com/blog/cache-tags-and-purge-api-on-netlify/)
- [Introducing Netlify Functions 2.0](https://www.netlify.com/blog/introducing-netlify-functions-2-0/)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,18 @@ import type { Config, Context } from "@netlify/edge-functions";
export default async (request: Request, context: Context) => {
const response = await context.next();

// look for existing "ab-test-bucket" cookie
const bucketName = "ab-test-bucket";
const bucket = context.cookies.get(bucketName);

// return here if we find a cookie
if (bucket) {
return response;
}

// if no "ab-test-bucket" cookie is found, assign the user to a bucket
// in this example we're using two buckets (a, b) with an equal weighting of 50/50
const weighting = 0.5;

// get a random number between (0-1)
// this is a basic example and you may want to experiment
const random = Math.random();
const newBucketValue = random <= weighting ? "A" : "B";

// set the new "ab-test-bucket" cookie
context.cookies.set({
name: bucketName,
value: newBucketValue,
Expand All @@ -32,5 +25,5 @@ export default async (request: Request, context: Context) => {
};

export const config: Config = {
path: "/*",
path: "/primitives/edge-functions/ab-testing",
};
31 changes: 0 additions & 31 deletions netlify/edge-functions/get-geo.ts

This file was deleted.

11 changes: 11 additions & 0 deletions netlify/edge-functions/get-geo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Config, Context } from "@netlify/edge-functions";

export default async (request: Request, context: Context) => {
return Response.json({
geo: context.geo,
});
};

export const config: Config = {
path: "/geolocation",
};
23 changes: 23 additions & 0 deletions netlify/functions/blob.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Config } from "@netlify/functions";
import { getStore } from "@netlify/blobs";

export default async (req: Request) => {
const store = getStore("ntl-workshop-todos");

if (req.method === "GET") {
const todos = await store.get("todos", { type: "json" });
return Response.json(todos || [], { status: 200 });
}

if (req.method === "PUT") {
const body = await req.json();
await store.setJSON("todos", body);
return new Response("Todos updated", { status: 200 });
}

return new Response("Unsupported method", { status: 405 });
};

export const config: Config = {
path: "/.netlify/functions/blob",
};
19 changes: 0 additions & 19 deletions netlify/functions/blob.ts

This file was deleted.

27 changes: 27 additions & 0 deletions netlify/functions/dad-joke.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { Config } from "@netlify/functions";

export default async () => {
const URL = "https://icanhazdadjoke.com/";
const headers = {
Accept: "application/json",
Authorization: Netlify.env.get("DAD_JOKE_API_KEY") || "",
};

try {
const response = await fetch(URL, { headers });
if (!response.ok) {
console.error(`HTTP error! status: ${response.status}`);
}
const json = await response.json();
console.log(json);
return Response.json(json);
} catch (error) {
console.error("Failed to fetch a dad joke:", error);
return new Response("Internal server error", { status: 500 });
}
};

export const config: Config = {
method: "GET",
path: "/api/dad-joke",
};
28 changes: 0 additions & 28 deletions netlify/functions/dad-joke.ts

This file was deleted.

Empty file.
40 changes: 40 additions & 0 deletions netlify/functions/platform-specific.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { Config, Context } from "@netlify/functions";

const nintendo = {
products: [
{ id: "n1", name: "Nintendo Gameboy Advance", promo: "web" },
{ id: "n2", name: "Super Nintendo", promo: null },
{ id: "n3", name: "Nintendo 64", promo: "mobile" },
{ id: "n4", name: "Nintendo Gamecube", promo: null },
{ id: "n5", name: "Nintendo Switch", promo: "mobile" },
],
};

const sony = {
products: [
{ id: "s1", name: "Sony PSP", promo: "web" },
{ id: "s2", name: "Sony PlayStation 3", promo: "mobile" },
{ id: "s3", name: "Sony PlayStation 4", promo: "mobile" },
{ id: "s4", name: "Sony PlayStation 5", promo: "mobile" },
],
};

export default async (req: Request, context: Context) => {
const { platform } = context.params;

const allProducts = nintendo.products.concat(sony.products);

if (!platform) {
return Response.json(allProducts);
}

const filteredProducts = allProducts.filter(
(product) => product.promo === platform,
);
return Response.json(filteredProducts);
};

export const config: Config = {
method: "GET",
path: "/api/consoles{/:platform}?",
};
46 changes: 0 additions & 46 deletions netlify/functions/platform-specific.ts

This file was deleted.

22 changes: 22 additions & 0 deletions netlify/functions/purge-cache-tag.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Config } from "@netlify/functions";
import { purgeCache } from "@netlify/functions";

export default async (req: Request) => {
const url = new URL(req.url);
const cacheTag = url.searchParams.get("tag");
if (!cacheTag) {
return;
}

console.log("Purging tag: ", cacheTag);

await purgeCache({
tags: [cacheTag],
});

return new Response("Purged!", { status: 202 });
};

export const config: Config = {
path: "/api/purge-cache",
};
17 changes: 0 additions & 17 deletions netlify/functions/purge-cache-tag.ts

This file was deleted.

50 changes: 50 additions & 0 deletions netlify/functions/scheduled-functions.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { Config } from "@netlify/functions";
import { purgeCache } from "@netlify/functions";
import { getStore } from "@netlify/blobs";

const SF_LAT = 37.7749;
const SF_LONG = -122.4194;
const WEATHER_API_URL = `https://api.open-meteo.com/v1/forecast?latitude=${SF_LAT}&longitude=${SF_LONG}&current_weather=true&temperature_unit=fahrenheit`;

export default async () => {
console.log("Scheduled function starting...");

try {
// 1. Fetch Weather Data
console.log("Fetching weather data...");
const weatherRes = await fetch(WEATHER_API_URL);
if (!weatherRes.ok) {
throw new Error(`Failed to fetch weather: ${weatherRes.statusText}`);
}
const weatherData = await weatherRes.json();
const weatherSummary = {
temperature: weatherData.current_weather.temperature,
windspeed: weatherData.current_weather.windspeed,
unit: "F", // requested fahrenheit above
fetchedAt: new Date().toISOString(),
fullData: weatherData.current_weather
};

// 2. Update Blob Store
console.log("Updating Blob store...");
const store = getStore("weather-store");
await store.setJSON("sf", weatherSummary);

// 3. Purge Cache Tag
console.log("Purging cache tag: weather-sf");
await purgeCache({
tags: ["weather-sf"],
});

console.log("Scheduled function complete.");
return new Response("Weather updated and cache purged", { status: 200 });

} catch (error) {
console.error("Error in scheduled function:", error);
return new Response("Internal Server Error", { status: 500 });
}
};

export const config: Config = {
schedule: "@daily",
};
Loading