From 59c867bfbc2c075432e80435f01b2a80087cabd7 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Fri, 10 Jun 2022 19:31:26 +0200 Subject: [PATCH] rewrite URLs --- docs/.eleventy.js | 26 ++++++++++++-------------- docs/package.json | 8 +++++--- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/docs/.eleventy.js b/docs/.eleventy.js index 1683ebc6164..ed1ffb83162 100644 --- a/docs/.eleventy.js +++ b/docs/.eleventy.js @@ -19,10 +19,10 @@ module.exports = function(eleventyConfig) { * The site is loaded from /docs on eslint.org and so we need to adjust * the path prefix so URLs are evaluated correctly. * - * The path prefix is turned off for `npm start` and deploy previews so we can properly + * The path prefix is turned off for deploy previews so we can properly * see changes before deployed. */ - const pathPrefix = ["local-serve", "deploy-preview"].includes(process.env.CONTEXT) ? "" : "/docs"; + const pathPrefix = process.env.CONTEXT === "deploy-preview" ? "" : "/docs"; //------------------------------------------------------------------------------ // Filters @@ -364,25 +364,23 @@ module.exports = function(eleventyConfig) { /* * When we run `eleventy --serve`, Eleventy 1.x uses browser-sync to serve the content. - * By default, browser-sync will not serve `foo/bar.html` when we request `foo/bar`. - * Thus, we need to specify "html" in `server.serveStaticOptions.extensions` so that - * pretty links without `.html` can work in a local development environment. + * By default, browser-sync (more precisely, underlying serve-static) will not serve + * `foo/bar.html` when we request `foo/bar`. Thus, we need to rewrite URLs to append `.html` + * so that pretty links without `.html` can work in a local development environment. * - * Note that eleventy is doing a shallow merge into its own browser-sync config, - * so this will unfortunately completely overwrite `server` settings. - * https://github.com/11ty/eleventy/blob/v1.0.1/src/EleventyServe.js#L78-L91 - * Therefore, we also have to specify `baseDir` here. + * There's no need to rewrite URLs that end with `/`, because that already works well + * (server will return the content of `index.html` in the directory). + * URLs with a file extension, like main.css, main.js, sitemap.xml, etc. should not be rewritten */ eleventyConfig.setBrowserSyncConfig({ - server: { - baseDir: "_site", - serveStaticOptions: { - extensions: ["html"] + middleware: (req, res, next) => { + if (!/(?:\.[^/]+|\/)$/u.test(req.url)) { + req.url += ".html"; } + return next(); } }); - return { passthroughFileCopy: true, diff --git a/docs/package.json b/docs/package.json index 84bb061823a..cea6e81c072 100644 --- a/docs/package.json +++ b/docs/package.json @@ -14,7 +14,7 @@ "watch:eleventy": "eleventy --serve --port=2023", "build:sass": "sass --style=compressed src/assets/scss:src/assets/css --no-source-map", "build:eleventy": "npx @11ty/eleventy", - "start": "cross-env CONTEXT=local-serve npm-run-all build:sass --parallel watch:* --parallel images", + "start": "npm-run-all build:sass --parallel watch:* --parallel images", "build": "npm-run-all build:sass --parallel build:* --parallel images" }, "devDependencies": { @@ -24,7 +24,6 @@ "@11ty/eleventy-plugin-rss": "^1.1.1", "@11ty/eleventy-plugin-syntaxhighlight": "^3.1.2", "algoliasearch": "^4.12.1", - "cross-env": "^7.0.3", "dom-parser": "^0.1.6", "eleventy-plugin-nesting-toc": "^1.3.0", "eleventy-plugin-page-assets": "^0.3.0", @@ -39,5 +38,8 @@ "rimraf": "^3.0.2", "sass": "^1.52.1", "slugify": "^1.6.3" - } + }, + "engines": { + "node": ">=14.0.0" + } }