使用 NextJS 托管 Apple 验证文件

Hosting Apple verification file with NextJS

我正在使用 NextJS。

根据 here,我需要在我的应用程序上托管一个文件。

具体来说,需要托管在:

https://app.blah.com/.well-known/apple-developer-merchantid-domain-association

从该文件夹提供该文件的最佳方式是什么? URL 必须与上面完全相同。

当我尝试将它放在名为 .well-known 的顶级文件夹中时,它不起作用。我怀疑是因为在实际构建应用程序时没有提供它。

相反,如果我将文件放在那里,localhost/static/apple-developer-merchantid-domain-association 工作正常。

那么,我该如何做我需要做的事情呢?

解决方案:使用自定义服务器并像这样提供服务:

const express = require(`express`);
const next = require(`next`);
const path = require(`path`);
const compression = require(`compression`);

const env = require(`./config/environment`);

const { PORT } = env;

// On GAE, we want to use their port. Locally, we want to use our port.
const port = process.env.PORT || PORT;

const app = next({ dev: process.env.NODE_ENV !== `production` });
const handle = app.getRequestHandler();

const robotsOptions = {
  root: path.join(__dirname, `/static/`),
  headers: {
    "Content-Type": `text/plain;charset=UTF-8`,
  },
};

const faviconOptions = {
  root: path.join(__dirname, `/static/`),
};

const appleVerification = {
  root: path.join(__dirname, `/static/`),
};

app.prepare().then(() => {
  const server = express();
  server.use(compression());

  server.get(`/robots.txt`, (req, res) => {
    res.status(200).sendFile(`robots.txt`, robotsOptions);
  });

  server.get(`/favicon.ico`, (req, res) => res.status(200).sendFile(`favicon.ico`, faviconOptions));

  server.get(
    `/.well-known/apple-developer-merchantid-domain-association`,
    (req, res) => res.status(200)
      .sendFile(
        `apple-developer-merchantid-domain-association`,
        appleVerification,
      ),
  );

  server.get(`*`, (req, res) => handle(req, res));

  server.listen(port, (err) => {
    if (err) {
      throw err;
    }
    console.log(`Server running on port: ${port}.`);
  });
});

Next.js public 文件夹中的任何内容都可以在根目录下访问。所以将文件放在 /public/.well-known/apple-developer-merchantid-domain-association 中,然后可以在 yourdomain.com/.well-known/apple-developer-merchantid-domain-association.

中访问它