Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

permalink: false will break transform functions as implemented like example in docs, since outputPath is 'false' #897

Closed
ckot opened this issue Feb 3, 2020 · 2 comments

Comments

@ckot
Copy link

ckot commented Feb 3, 2020

Describe the bug

I recently added permalink: false to some templates and my transform function blew up an exception (listed below in screenshots section)

To Reproduce

  1. add the following transform function (example from docs)
const htmlmin = require("html-minifier");

module.exports = function(eleventyConfig) {
  eleventyConfig.addTransform("htmlmin", function(content, outputPath) {
    if( outputPath.endsWith(".html") ) {
      let minified = htmlmin.minify(content, {
        useShortDoctype: true,
        removeComments: true,
        collapseWhitespace: true
      });
      return minified;
    }

    return content;
  });
};
  1. add permalink: false to some template's data

Expected behavior

I'd expect that the content with permalink: false and for my transform function to work normally

Screenshots

$ eleventy
Writing dist/index.html from ./src/index.njk.
Error writing templates: (more in DEBUG output)
> Having trouble writing template: false

TemplateWriterWriteError was thrown
> outputPath.endsWith is not a function

TypeError was thrown:
TypeError: outputPath.endsWith is not a function
at Template. (/home/ckot/projects/work/11ty_test/.eleventy.js:7:24)
at Template.runTransforms (/usr/lib/node_modules/@11ty/eleventy/src/Template.js:387:29)
at Template.renderPageEntry (/usr/lib/node_modules/@11ty/eleventy/src/Template.js:548:28)
at process._tickCallback (internal/process/next_tick.js:68:7)
Problem writing Eleventy templates: (more in DEBUG output)

Environment:

  • OS and version: Ubuntu LTS 18.04
  • Eleventy Version: 0.10.0

Additional context

I suppose this is not a code bug but rather a documentation bug, although perhaps thinks which have permalink-false should't be passed through transforms, as they don't have an outputPath to begin with.

My fix was to simply test that outputPath is truthy, and that it's a string, prior to calling a string method on it. Regardless of whether there is a code fix for this, it would probably be good that the docs should look more like my solution, simply since it uses defensive programming.

const htmlmin = require("html-minifier");

module.exports = function(eleventyConfig) {
  eleventyConfig.addTransform("htmlmin", function(content, outputPath) {
   // test that outputPath is a string prior to doing anything with it.  setting permalink: false, will
   // set outputPath to 'false', and perhaps other settings may give unexpected outputPath values 
   if( outputPath && "string" === typeof(outputPath) && outputPath.endsWith(".html") ) {
      let minified = htmlmin.minify(content, {
        useShortDoctype: true,
        removeComments: true,
        collapseWhitespace: true
      });
      return minified;
    }

    return content;
  });
};
@chrissy-dev
Copy link

Just to note I had the same issue as @ckot has mentioned, I used a similar workaround/fix.

@zachleat
Copy link
Member

This is a duplicate of #653—please follow along over there!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants