Subh
12/14/2021, 11:27 AMawait fetch(NEXT_PUBLIC_NHOST_GRAPHQL_API
this will not work if defined in Production environment variables?
Asking because NEXT_PUBLIC_NHOST_GRAPHQL_API is not defined
got this error in production functions.geelen
12/14/2021, 12:28 PMGreg Brimble | Cloudflare Pages
12/14/2021, 12:57 PMenv.NEXT_PUBLIC_NHOST_GRAPHQL_API
JustinNoel
12/14/2021, 1:15 PMexport const onRequestGet: PagesFunction<Env, "params", Data> = async ({ env, params }) => { ... }
I am now getting Cannot find name 'PagesFunction'.ts(2304)
However, I've setup my tsconfing.json
as follows per the @cloudflare/workers-types
instructions:
{
"compilerOptions": {
"alwaysStrict": true,
"esModuleInterop": true,
"jsx": "react-jsx",
"jsxImportSource": "preact",
"moduleResolution": "node",
"noImplicitAny": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"resolveJsonModule": true,
"strict": true,
"target": "ES2020",
"module": "CommonJS",
"lib": [
"ES2020"
],
"types": [
"@cloudflare/workers-types"
]
},
"include": [
"dev/**/*",
"src/**/*",
"functions/**/*"
],
"exclude": [
"node_modules"
]
}
It's now just Visual Studio Code either. If I run yarn tsc
, I get the following:
functions/api/images/[id].ts:9:28 - error TS2304: Cannot find name 'PagesFunction'.
9 export const onRequestGet: PagesFunction<
~~~~~~~~~~~~~
functions/api/images/[id].ts:13:14 - error TS7031: Binding element 'env' implicitly has an 'any' type.
13 > = async ({ env, params }) => {
~~~
functions/api/images/[id].ts:13:19 - error TS7031: Binding element 'params' implicitly has an 'any' type.
13 > = async ({ env, params }) => {
~~~~~~
Anyone have any ideas?geelen
12/14/2021, 1:44 PMJustinNoel
12/14/2021, 2:14 PM"@cloudflare/workers-types@^2.2.2":
version "2.2.2"
resolved "https://registry.yarnpkg.com/@cloudflare/workers-types/-/workers-types-2.2.2.tgz#1bced16bba801d7af987da835467545bb5cc7ac6"
integrity sha512-kaMn2rueJ0PL1TYVGknTCh0X0x0d9G+FNXAFep7/4uqecEZoQb/63o6rOmMuiqI09zLuHV6xhKRXinokV/MY9A==
"@cloudflare/workers-types@^3.2.0":
version "3.2.0"
resolved "https://registry.yarnpkg.com/@cloudflare/workers-types/-/workers-types-3.2.0.tgz#df300466f5f8a03b205bdd533990017b0538496e"
integrity sha512-y0+f7QeB5/fMMdU0wSwvBB18yE9kAD2s7Wben8a4uI4f/EJyE+eJrai5QO52Pq8EmWP0vRpKqZh0qU857WhY2A==
Now, I realize why this happened. I installed Miniflare as a dependency so I could load some fake data into my KV namespace for developing locally. Miniflare must have added the old version of @cloudflare/workers-types
.
Removing Miniflare (and the old types) did the trick.
So, I'll have to use Miniflare a bit differently. Thanks so much!robinio
12/14/2021, 3:40 PMNevi | Pages
12/14/2021, 5:23 PMjschlesser
12/14/2021, 9:04 PMpxkq
12/14/2021, 9:31 PM├── functions
│ ├── _middleware.ts # Applies to all routes
│ ├── ...
│ └── todos
│ ├── _middleware.ts # Adds extra middleware to /todos/**
│ ├── ...
└── ...
I assumed the order of execution was top level first, deeper level next. I have an errorHandler on the top level, but it's executing first the inner middleware.
Any idea why could that be? Using wrangler pages dev (alpha)jschlesser
12/14/2021, 9:49 PMreturn await next()
in both spots. If your top level isn't awaiting next, it might cause the behavior you are seeing.pxkq
12/14/2021, 9:56 PMreturn await next()
in both. But I have them as in the example using arrays:
//top level
export const onRequest = [
errorHandler,
]
//next level
export const onRequest = [
secondMiddleware,
]
Maybe that affects the awating? I'll try move it aroundjschlesser
12/14/2021, 10:00 PMjschlesser
12/14/2021, 10:01 PMimport {setup as kvsetup } from './lib/global/KV'
const errorHandler = async ({ next }) => {
try {
console.log('before errorHandler')
return await next()
} catch (err) {
return new Response(`${err.message}\n${err.stack}`, { status: 500 })
}
}
const setup = async({env, next}) => {
kvsetup(env)
return await next()
}
export const onRequest = [
errorHandler,
setup
]
Level Down
const consoleHello = async ({ next }) => {
try {
console.log('hello from api middleware')
return await next()
} catch (err) {
return new Response(`${err.message}\n${err.stack}`, { status: 500 })
}
}
export const onRequest = [
consoleHello
]
jschlesser
12/14/2021, 10:02 PMpxkq
12/14/2021, 10:06 PMhello from api middleware
before errorHandler
actual endpoint
pxkq
12/14/2021, 10:08 PM├── functions
│ ├── _middleware.ts # THIS ONE IS TOP LEVEL (before errorHanlder)
│ ├── ...
│ └── todos
│ ├── _middleware.ts # THIS IS DEEP LEVEL (hello from api middleware)
│ ├── ...
└── ...
jschlesser
12/14/2021, 10:08 PMbefore errorHandler
hello from api middleware
actual endpoint
jschlesser
12/14/2021, 10:09 PMpxkq
12/14/2021, 10:09 PMpxkq
12/14/2021, 10:09 PMjschlesser
12/14/2021, 10:09 PMjschlesser
12/14/2021, 10:10 PMpxkq
12/14/2021, 10:11 PMjschlesser
12/14/2021, 10:11 PMpxkq
12/14/2021, 10:12 PMpxkq
12/14/2021, 10:17 PM├── functions
│ ├── _middleware.ts # THIS ONE IS TOP LEVEL (before errorHanlder)
│ ├── ...
│ └── todos
│ ├── _middleware.ts # THIS IS DEEP LEVEL (hello from api middleware)
│ ├── ...
└── ...
But as:
├── functions
| |-- api/
│ ├── _middleware.ts # THIS ONE IS TOP LEVEL (before errorHanlder)
│ ├── ...
│ └── todos
│ ├── _middleware.ts # THIS IS DEEP LEVEL (hello from api middleware)
│ ├── ...
└── ...
That way middlewares get invertedjschlesser
12/14/2021, 10:24 PMhello from api middleware
before errorHandler
actual endpoint, hey
jschlesser
12/14/2021, 10:24 PM