Skip to content

Commit

Permalink
Implement minimal ISR with Deno
Browse files Browse the repository at this point in the history
  • Loading branch information
notadilnaqvi committed Jun 2, 2024
1 parent e297320 commit 89b3853
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
33 changes: 18 additions & 15 deletions server.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import { serveDir } from "https://deno.land/[email protected]/http/file_server.ts";

async function updateIndexHtml() {
const indexHtmlText = await Deno.readTextFile("index.html");

const currentTime = new Date().toLocaleString();

const updatedIndexHtmlText = indexHtmlText.replace(
"{{ LAST_UPDATED }}",
currentTime,
);

await Deno.writeTextFile("index.html", updatedIndexHtmlText);
}
// Create the initial index.html
await generateIndexHtml();

// Serve the static files
Deno.serve(async (req) => {
return await serveDir(req, {
fsRoot: "./static/",
fsRoot: Deno.cwd() + "/static/",
});
});

Deno.cron("Update last updated", "*/10 * * * *", async () => {
await updateIndexHtml();
// Generate index.html every minute with latest data
Deno.cron("Update last updated", "* */1 * * *", async () => {
await generateIndexHtml();
});

async function generateIndexHtml() {
const currentTime = new Date().toLocaleString();
const htmlTemplate = await Deno.readTextFile(
Deno.cwd() + "/static/.template.html",
);

const indexHtml = htmlTemplate.replace("{{ LAST_UPDATED }}", currentTime);

await Deno.writeTextFile(Deno.cwd() + "/static/index.html", indexHtml);
}
2 changes: 1 addition & 1 deletion static/index.html → static/.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ <h2>Find me on the interwebs</h2>
Thanks for visiting :)
</marquee>
<hr />
<p>{{ LAST_UPDATED }}</p>
<p>Last updated: {{ LAST_UPDATED }}</p>
</main>
</body>
</html>

1 comment on commit 89b3853

@deno-deploy
Copy link

@deno-deploy deno-deploy bot commented on 89b3853 Jun 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to deploy:

UNCAUGHT_EXCEPTION

PermissionDenied: Requires write access to "/src/static/index.html", run again with the --allow-write flag
    at writeFile (ext:deno_fs/30_fs.js:936:13)
    at Object.writeTextFile (ext:deno_fs/30_fs.js:978:12)
    at generateIndexHtml (file:///src/server.ts:26:14)
    at eventLoopTick (ext:core/01_core.js:168:7)
    at async file:///src/server.ts:4:1

Please sign in to comment.