Skip to content

Problem with the Vike programmatic API in an API route #3094

@robin-ln

Description

@robin-ln

Context

  • Project: Vike with Vue (vike, vue-vike) and vike-photon (with hone).
  • Goal: an API route that triggers the application's prerender.
  • Behavior: works fine in development. In production, a non-blocking error appears at server startup.

Startup error :

➜ Listening on: http://localhost:3000/ (all interfaces)
Server running at http://localhost:3000/
Error: [[email protected]][Bug] You stumbled upon a Vike bug. Go to https://github.com/vikejs/vike/issues/new?template=bug.yml and copy-paste this error. A maintainer will fix the bug (usually within 24 hours).
    at onSetupRuntime (file:///***/node_modules/vike/dist/utils/assertSetup.js:38:9)
    at createGlobalContext (file:///***/node_modules/vike/dist/server/runtime/globalContext.js:460:5)
    at async loadProdBuildEntry (file:///***/node_modules/vike/dist/server/runtime/globalContext.js:279:5)
    at async setGlobalContext_prodBuildEntry (file:///***/node_modules/vike/dist/server/runtime/globalContext.js:291:5)

API endpoint

import { prerender } from 'vike/api'

export default async function handler(_request: Request) {


  try {
    await prerender()
    return Response.json({
      revalidate: true,
      date: String(new Date()),
    })
  } catch (error) {
    return Response.json({
      revalidate: false,
      date: String(new Date()),
    })
  }
}

vike-photon configuration :

import type { Config } from 'vike/types'
import vikeVue from 'vike-vue/config'
import vikePhoton from 'vike-photon/config'

export const config: Config = {
  extends: [vikeVue, vikePhoton],
  prerender: {
    enable: true,
    keepDistServer: true,
  },
  photon: {
    entry: './src/server/index.ts',
    entries: {
      revalidate: {
        id: './src/server/api/revalidate.ts',
        route: '/api/revalidate',
      },
    },
  },
}

Note

Expectation

  • Eliminate the startup error when running the server in production.
  • Or get a best solution for force a prerender

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions