如何从正确的URL加载静态内容?

时间:2019-05-22 14:49:18

标签: javascript internationalization router next.js

默认情况下,如果使用i18n库而不使用下一路由库,则无论使用哪种语言,我的意思是URL都带有前缀,例如http://localhost:8080/some-pathhttp://localhost:8080/de/some-path。在这种情况下,会从http://localhost:8080/static/font/whatever.woff不断加载静态内容。而且,如果添加了下一路由并使用了相同的URL,则会从http://localhost:8080/de/static/font/whatever.woff加载静态内容。问题是我正在使用Varnish,并且添加URL规则不是一个好习惯

这是具有Varnish和nginx的Linux 18.04服务器。

node.js代码:

const express = require('express');
const cookieParser = require('cookie-parser');
const next = require('next');
const nextI18NextMiddleware = require('next-i18next/middleware');
const compression = require('compression');
const helmet = require('helmet');
const path = require('path');

const nextI18next = require('./i18n');
const routes = require('./routes');

const port = process.env.PORT || 3000;
const app = next({ dev: process.env.NODE_ENV !== 'production' });
const handler = routes.getRequestHandler(app);

(async () => {
  await app.prepare();
  const server = express();

  server.use(helmet());
  server.use(cookieParser());
  server.use(compression());
  server.use(nextI18NextMiddleware(nextI18next));
  server.use(handler);

  server.get('/service-worker.js', (req, res) =>
    app.serveStatic(req, res, path.join(__dirname, '.next', '/service-worker.js')),
  );

  await server.listen(port);
  console.log(`> Ready on http://localhost:${port}`);
})();

我希望嵌入到javascript中的资源总是会从例如http://localhost:8080/static/font URl与应用程序上选择的语言无关。

0 个答案:

没有答案