Merge pull request #54 from syumai/use-assets-gen-cmd-in-pages-functions

use workers-assets-gen command in pages functions example
This commit is contained in:
syumai 2023-04-30 13:35:15 +09:00 committed by GitHub
commit 26d2a5a0fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 22 deletions

View File

@ -4,7 +4,7 @@ dev:
.PHONY: build .PHONY: build
build: build:
mkdir -p build go run ../../cmd/workers-assets-gen
tinygo build -o ./build/app.wasm -target wasm ./... tinygo build -o ./build/app.wasm -target wasm ./...
.PHONY: publish .PHONY: publish

View File

@ -1,24 +1,6 @@
import "../../assets/polyfill_performance.js";
import "../../assets/wasm_exec.js";
import mod from "../build/app.wasm"; import mod from "../build/app.wasm";
import * as imports from "../build/shim.mjs"
const go = new Go(); imports.init(mod);
const readyPromise = new Promise((resolve) => { export const onRequest = imports.onRequest;
globalThis.ready = resolve;
});
const load = WebAssembly.instantiate(mod, go.importObject).then((instance) => {
go.run(instance);
return instance;
});
export const onRequest = async (ctx) => {
await load;
await readyPromise;
const {
request,
env,
} = ctx;
return handleRequest(request, { env, ctx });
}

View File

@ -1,4 +1,5 @@
name = "pages-functions" name = "pages-functions"
compatibility_date = "2023-04-30"
[build] [build]
command = "make build" command = "make build"

View File

@ -27,3 +27,10 @@ export async function scheduled(event, env, ctx) {
await run(); await run();
return runScheduler(event, { env, ctx }); return runScheduler(event, { env, ctx });
} }
// onRequest handles request to Cloudflare Pages
export async function onRequest(ctx) {
await run();
const { request, env } = ctx;
return handleRequest(request, { env, ctx });
}