Skip to content

Commit

Permalink
Update Static Publisher for JavaScript to redirect apex domain to www…
Browse files Browse the repository at this point in the history
… version and give another shot at GitHub action for deployment working
  • Loading branch information
arunsathiya committed Jan 27, 2024
1 parent 13e47ff commit 5b11299
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ jobs:
- name: Build project
run: npm run build

deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Install Fastly dependencies
run: npm install
working-directory: ./compute-js

- name: fastly compute deploy
uses: fastly/compute-actions@v5
with:
project_directory: compute-js
project_directory: compute-js
comment: “Deployed via GitHub Actions”
env:
FASTLY_API_TOKEN: ${{ secrets.FASTLY_API_TOKEN }}
29 changes: 22 additions & 7 deletions compute-js/src/index.js
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 });
}
5 changes: 4 additions & 1 deletion tsconfig.json
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"
]
}

0 comments on commit 5b11299

Please sign in to comment.