-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Static Publisher for JavaScript to redirect apex domain to www…
… version and give another shot at GitHub action for deployment working
- Loading branch information
1 parent
13e47ff
commit 5b11299
Showing
3 changed files
with
31 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,33 @@ | ||
/// <reference types="@fastly/js-compute" /> | ||
import { getServer } from '../static-publisher/statics.js'; | ||
import { getServer } from "../static-publisher/statics.js"; | ||
const staticContentServer = getServer(); | ||
|
||
// eslint-disable-next-line no-restricted-globals | ||
addEventListener("fetch", (event) => event.respondWith(handleRequest(event))); | ||
|
||
async function handleRequest(event) { | ||
const req = event.request; | ||
const host = req.headers.get("Host"); | ||
|
||
// Check for apex domain and redirect to www | ||
if (host && !host.startsWith("www.")) { | ||
const redirectURL = new URL(req.url); | ||
redirectURL.hostname = `www.${host}`; | ||
|
||
const response = await staticContentServer.serveRequest(event.request); | ||
return new Response(null, { | ||
headers: { | ||
Location: redirectURL.toString(), | ||
"Cache-Control": "max-age=86400", | ||
}, | ||
status: 301, | ||
}); | ||
} | ||
|
||
// Serve static content | ||
const response = await staticContentServer.serveRequest(req); | ||
if (response != null) { | ||
return response; | ||
} | ||
|
||
// Do custom things here! | ||
// Handle API requests, serve non-static responses, etc. | ||
|
||
return new Response('Not found', { status: 404 }); | ||
// Return a 404 response if the content is not found | ||
return new Response("Not found", { status: 404 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
{ | ||
"extends": "astro/tsconfigs/strict" | ||
"extends": "astro/tsconfigs/strict", | ||
"exclude": [ | ||
"compute-js" | ||
] | ||
} |