Skip to content

Strip BOM character from templates saved as 'UTF-8 with signature' #77

Closed
@jimnoble

Description

@jimnoble

By default, my IDE saves text files as 'UTF-8 with signature', which includes a BOM character at the beginning of the file.

Since node's file API doesn't strip this character out (see nodejs/node-v0.x-archive#1918), this ends up leaking out of my template .handlebars file all the way into the HTML response.

This caused me some great confusion since Chrome's debugger doesnt actually show the character, but nevertheless allows it to mess the page layout up.

I can change the encoding to 'UTF-8 without signature', but this is kludgey, and I have to explicitly do it for every handlebars template file I make.

The fix is really simple, one line of code added around line 327 of express-handlebars.js:

fs.readFile(filePath, 'utf8', function (err, file) {
    if (!err) {

        // BEGIN FIX 

        // Remove BOM character if there is one at the start of the file.

        if(file.charCodeAt(0) == 65279) file = file.substr(1);

        // END FIX

        fileCache[filePath] = file;
    }

Can we add it to the official source so I don't need to patch after every update?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions