Skip to content

Commit

Permalink
Add EF caching example
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlee85 committed Oct 11, 2023
1 parent 07a8d00 commit ff35a6c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 21 deletions.
9 changes: 9 additions & 0 deletions examples/v7-edge-functions/edgio.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ module.exports = {
},
],
},
{
name: 'echo',
override_host_header: 'http-echo.raees.me',
hosts: [
{
location: 'http-echo.raees.me',
},
],
},
{
name: 'planetscale',
override_host_header: 'aws.connect.psdb.cloud',
Expand Down
29 changes: 29 additions & 0 deletions examples/v7-edge-functions/functions/general/caching.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import createFetchForOrigin from '../../utils/createFetchForOrigin';
import '../../utils/polyfills/URL';

const fetch = createFetchForOrigin('echo');

export async function handleHttpRequest(request, context) {
const { method } = request;
const body = method === 'POST' ? await request.arrayBuffer() : null;

// get the headers from the incoming request, removing the content-length header
const headers = Object.fromEntries(
[...request.headers.entries()].filter(([key]) => key !== 'content-length')
);

const newRequest = new Request('https://http-echo.raees.me', {
method,
headers,
body,
});

const response = await fetch(newRequest);

// add a cache-control header for GET requests
if (method === 'GET') {
response.headers.set('cache-control', 's-maxage=600');
}

return response;
}
34 changes: 17 additions & 17 deletions examples/v7-edge-functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions examples/v7-edge-functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
},
"license": "MIT",
"devDependencies": {
"@edgio/cli": "^7.2.7",
"@edgio/core": "^7.2.7",
"@edgio/devtools": "^7.2.7",
"@edgio/prefetch": "^7.2.7",
"@edgio/cli": "^7.2.8",
"@edgio/core": "^7.2.8",
"@edgio/devtools": "^7.2.8",
"@edgio/prefetch": "^7.2.8",
"dotenv": "^16.3.1"
},
"repository": "[email protected]:edgio-docs/edgio-v7-edge-functions-example.git",
Expand Down
12 changes: 12 additions & 0 deletions examples/v7-edge-functions/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,21 @@ export default new Router()
.match('/example/redirects(.*)', {
edge_function: './functions/general/redirect.js',
})
.match('/example/caching', {
caching: {
enable_caching_for_methods: ['GET', 'POST'],
},
edge_function: './functions/general/caching.js',
})
.match('/example/planetscale-database', {
edge_function: './functions/database/planetscale/index.js',
})
.match('/example/upstash-database', {
edge_function: './functions/database/upstash/index.js',
})
.match('/bug', {
caching: {
enable_caching_for_methods: ['GET', 'POST'],
},
edge_function: './functions/general/bug.js',
});

0 comments on commit ff35a6c

Please sign in to comment.