Skip to content

Commit

Permalink
rewrite URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic committed Jun 10, 2022
1 parent c5ba8c1 commit 59c867b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
26 changes: 12 additions & 14 deletions docs/.eleventy.js
Expand Up @@ -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
Expand Down Expand Up @@ -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,

Expand Down
8 changes: 5 additions & 3 deletions docs/package.json
Expand Up @@ -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": {
Expand All @@ -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",
Expand All @@ -39,5 +38,8 @@
"rimraf": "^3.0.2",
"sass": "^1.52.1",
"slugify": "^1.6.3"
}
},
"engines": {
"node": ">=14.0.0"
}
}

0 comments on commit 59c867b

Please sign in to comment.