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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for scope to be of type Context to fix rendering in Liquid #35

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

gracexu94
Copy link

@gracexu94 gracexu94 commented Oct 19, 2023

Description

This PR attempts to add support for the scope variable when it is of type Context. Without these changes, I was unable to use this plugin to generate SEO tags for my Eleventy site which uses Liquid templates.

Motivation and Context

Attempting to use this plugin on an Eleventy site with Liquid templates resulted in runtime errors (snapshot below). It seems like other people have also run into this issue when attempting to use this plugin.

[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] 1. Having trouble writing to "_site/contact/index.html" from "./src/pages/contact.md" (via EleventyTemplateError)
[11ty] 2. Cannot read property '0' of undefined, line:9, col:1 (via RenderError)
[11ty] 3. Cannot read property '0' of undefined (via TypeError)
[11ty] 
[11ty] Original error stack trace: TypeError: Cannot read property '0' of undefined
[11ty]     at OpenGraph.liquidRender (/Users/gracexu/Projects/digital-services-coalition/node_modules/eleventy-plugin-seo/src/Tags/OpenGraph.js:10:47)
[11ty]     at Object.render (/Users/gracexu/Projects/digital-services-coalition/node_modules/eleventy-plugin-seo/src/Tags/BaseTag.js:41:37)
[11ty]     at Object.render (/Users/gracexu/Projects/digital-services-coalition/node_modules/liquidjs/dist/liquid.node.cjs.js:2023:41)
[11ty]     at render.next (<anonymous>)
[11ty]     at toPromise (/Users/gracexu/Projects/digital-services-coalition/node_modules/liquidjs/dist/liquid.node.cjs.js:462:32)
[11ty]     at async toPromise (/Users/gracexu/Projects/digital-services-coalition/node_modules/liquidjs/dist/liquid.node.cjs.js:470:25)
[11ty]     at async SEO.liquidRender (/Users/gracexu/Projects/digital-services-coalition/node_modules/eleventy-plugin-seo/src/Tags/SEO.js:7:22)
[11ty]     at async toPromise (/Users/gracexu/Projects/digital-services-coalition/node_modules/liquidjs/dist/liquid.node.cjs.js:470:25)
[11ty]     at async toPromise (/Users/gracexu/Projects/digital-services-coalition/node_modules/liquidjs/dist/liquid.node.cjs.js:470:25)
[11ty]     at async TemplateLayout.render (/Users/gracexu/Projects/digital-services-coalition/node_modules/@11ty/eleventy/src/TemplateLayout.js:236:25)

After some digging, I realized that the error was coming from the references to scope.contexts[0] in the liquidRender functions. Printing scope to the console revealed that the object didn't have a field called contexts, hence the "Cannot read property '0' of undefined" error.

Click to see a snapshot of what the `scope` object looked like on my local ``` Context { scopes: [ {} ], registers: {}, sync: false, opts: { root: [ 'src/_includes', 'src' ], layouts: [ 'src/_includes', 'src' ], partials: [ 'src/_includes', 'src' ], relativeReference: true, jekyllInclude: false, cache: undefined, extname: '.liquid', fs: [Object: null prototype] { exists: [AsyncFunction: exists], readFile: [Function: readFile], existsSync: [Function: existsSync], readFileSync: [Function: readFileSync], resolve: [Function: resolve], fallback: [Function: fallback], dirname: [Function: dirname], contains: [Function: contains], sep: '/' }, dynamicPartials: true, jsTruthy: false, dateFormat: '%A, %B %-e, %Y at %-l:%M %P %z', trimTagRight: false, trimTagLeft: false, trimOutputRight: false, trimOutputLeft: false, greedy: true, tagDelimiterLeft: '{%', tagDelimiterRight: '%}', outputDelimiterLeft: '{{', outputDelimiterRight: '}}', preserveTimezones: false, strictFilters: true, strictVariables: false, ownPropertyOnly: true, lenientIf: false, globals: {}, keepOutputType: false, operators: { '==': [Function: equal], '!=': [Function: !=], '>': [Function: >], '<': [Function: <], '>=': [Function: >=], '<=': [Function: <=], contains: [Function: contains], not: [Function: not], and: [Function: and], or: [Function: or] }, outputEscape: undefined }, globals: {}, environments: { eleventy: { version: '2.0.1', generator: 'Eleventy v2.0.1', env: [Object] }, pkg: { name: 'digital-services-coalition', version: '1.0.0', description: '', main: 'index.js', scripts: [Object], repository: [Object], keywords: [], author: '', license: 'ISC', bugs: [Object], homepage: 'https://github.com/DSCoalition/dsc-11ty-cms', devDependencies: [Object], dependencies: [Object] }, title: 'Story', layout: 'layouts/story.liquid', permalink: '/story/', heroTitle: 'Answering the call by building a community.', eleventyNavigation: { key: 'Story', order: 3 }, page: { date: 2023-08-28T16:46:58.708Z, inputPath: './src/pages/story.md', fileSlug: 'story', filePathStem: '/pages/story', outputFileExtension: 'html', templateSyntax: 'liquid,md', url: '/story/', outputPath: '_site/story/index.html' }, collections: { all: [Array], boardMemberItem: [Array], orgItem: [Array] }, content: '\n' + '
\n' + '
' }, strictVariables: false, ownPropertyOnly: true } ```

I checked the LiquidJS changelog and it does look like a recent change was made in 2022 to support scopes of type Context.

To maintain backwards compatibility while fixing the issue I just added a check to set the context to scope.environments (the user passed in scope for the Context type) when scope.contexts is undefined.

How Has This Been Tested?

I added some tests for the new use case and made sure all tests passed. I confirmed that after making these changes I was able to generate SEO tags and use this plugin on my Eleventy site.
Screen Shot 2023-10-19 at 5 51 45 PM

Types of Changes

  • Bug fix (non-breaking change that fixes an issue)
    - [ ] New feature (non-breaking change that adds functionality)
    - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My code follows the code style of this project.
    - [ ] My change requires a change to the documentation.
    - [ ] I have updated the documentation accordingly.

Thanks for your work creating and maintaining this plugin! 馃槃

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