serverPluginServeStatic.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.serveStaticPlugin = exports.seenUrls = void 0;
  7. const fs_1 = __importDefault(require("fs"));
  8. const path_1 = __importDefault(require("path"));
  9. const chalk_1 = __importDefault(require("chalk"));
  10. const send = require('koa-send');
  11. const debug = require('debug')('vite:history');
  12. exports.seenUrls = new Set();
  13. exports.serveStaticPlugin = ({ root, app, resolver, config }) => {
  14. app.use(async (ctx, next) => {
  15. // short circuit requests that have already been explicitly handled
  16. if (ctx.body || ctx.status !== 404) {
  17. return;
  18. }
  19. // warn non-root references to assets under /public/
  20. if (ctx.path.startsWith('/public/') && resolver.isAssetRequest(ctx.path)) {
  21. console.error(chalk_1.default.yellow(`[vite] files in the public directory are served at the root path.\n` +
  22. ` ${chalk_1.default.blue(ctx.path)} should be changed to ${chalk_1.default.blue(ctx.path.replace(/^\/public\//, '/'))}.`));
  23. }
  24. // handle possible user request -> file aliases
  25. const expectsHtml = ctx.headers.accept && ctx.headers.accept.includes('text/html');
  26. if (!expectsHtml) {
  27. const filePath = resolver.requestToFile(ctx.path);
  28. if (filePath !== ctx.path &&
  29. fs_1.default.existsSync(filePath) &&
  30. fs_1.default.statSync(filePath).isFile()) {
  31. await ctx.read(filePath);
  32. }
  33. }
  34. await next();
  35. // the first request to the server should never 304
  36. if (exports.seenUrls.has(ctx.url) && ctx.fresh) {
  37. ctx.status = 304;
  38. }
  39. exports.seenUrls.add(ctx.url);
  40. });
  41. app.use(require('koa-etag')());
  42. app.use(require('koa-static')(root));
  43. app.use(require('koa-static')(path_1.default.join(root, 'public')));
  44. // history API fallback
  45. app.use(async (ctx, next) => {
  46. if (ctx.status !== 404) {
  47. return next();
  48. }
  49. if (ctx.method !== 'GET') {
  50. debug(`not redirecting ${ctx.url} (not GET)`);
  51. return next();
  52. }
  53. const accept = ctx.headers && ctx.headers.accept;
  54. if (typeof accept !== 'string') {
  55. debug(`not redirecting ${ctx.url} (no headers.accept)`);
  56. return next();
  57. }
  58. if (accept.includes('application/json')) {
  59. debug(`not redirecting ${ctx.url} (json)`);
  60. return next();
  61. }
  62. if (!accept.includes('text/html')) {
  63. debug(`not redirecting ${ctx.url} (not accepting html)`);
  64. return next();
  65. }
  66. debug(`redirecting ${ctx.url} to /index.html`);
  67. try {
  68. await send(ctx, `index.html`, { root });
  69. }
  70. catch (e) {
  71. ctx.url = '/index.html';
  72. ctx.status = 404;
  73. return next();
  74. }
  75. });
  76. };
  77. //# sourceMappingURL=serverPluginServeStatic.js.map