const functions = require("firebase-functions")
const { Nuxt } = require("nuxt")
const express = require("express")
const { SitemapStream, streamToPromise } = require("sitemap")
const app = express()

let sitemap
app.get("/sitemap.xml", (req, res) => {
  res.header("Content-Type", "application/xml")
  if (sitemap) {
    res.send(sitemap)
    return
  }

  try {
    const smStream = new SitemapStream({
      hostname: "https://shikibetsuhyo.yysaki.com",
    })

    const urls = ["shiren", "shiren2", "shiren3", "shiren4", "shiren5"]
      .map((name) => `/${name}/`)
      .concat(["/"])
    urls.forEach((url) => {
      smStream.write({ url, changeFreq: "monthly" })
    })
    smStream.end()

    streamToPromise(smStream)
      .then((sm) => (sitemap = sm))
      .catch((e) => {
        throw e
      })
    smStream.pipe(res).on("error", (e) => {
      throw e
    })
  } catch (e) {
    console.error(e)
    res.status(500).end()
  }
})

const nuxt = new Nuxt({ buildDir: "nuxt", dev: false })

function handleRequest(req, res) {
  res.set("Cache-Control", "public, max-age=300, s-maxage=600")
  return nuxt.render(req, res)
}

app.use(handleRequest)
exports.ssr = functions.https.onRequest(app)

@@ -1002,164 +1002,29 @@
  %7D)%0A
-%0Afunction handleRequest(req, res) %7B%0A  res.set(%22Cache-Control%22, %22public, max-age=300, s-maxage=600%22)%0A  return nuxt.render(req, res)%0A%7D%0A%0Aapp.use(handleRequest)
+app.use(nuxt.render)%0A
 %0Aexp

Disable caching