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

Can't reuse a template file as a layout file #161

Open
ErisDS opened this issue Sep 6, 2019 · 2 comments
Open

Can't reuse a template file as a layout file #161

ErisDS opened this issue Sep 6, 2019 · 2 comments
Labels

Comments

@ErisDS
Copy link
Member

ErisDS commented Sep 6, 2019

This only affects production mode where templates get cached.

Assuming you have a default.hbs file which is your base layout file.

In the case that you load and use default.hbs as a template before loading it as a layout, it will break the layout of all other pages that use the default.hbs layout. This issue will cause what appear to be intermittent layout bugs, because it depends on the order the files get used in...

If you load a page that uses default.hbs as a template first, you'll then get "Cannot read property 'match' of undefined" when trying to load the page that uses default.hbs as a layout.

If you load a page that uses default.hbs as a layout first, you'll get a blank page when trying to load any pages that use it as a template.

This is a super crazy edgecase and I don't think it necessarily needs fixing, just raising it as it caused TryGhost/Ghost#10990 and it's useful to know if you're working with express-hbs.

There's a lot more detail about the weird behaviour in TryGhost/Ghost#10990

ErisDS added a commit to TryGhost/Ghost that referenced this issue Sep 6, 2019
fixes #10990

- Changed the static router to throw a 400 error for a missing template file, rather than falling back to using the default.hbs file
- Falling back is weird and hard to understand, but throwing an error makes it clear that the user has to provide the matching template
- The new error reads 'Missing template [filename].hbs for route "[route]".'

Assume you have a route.yaml file something like:

```
routes:
  /: home
```

- In Ghost v2, if you don't have a home.hbs template, Ghost falls back to using the default.hbs file if it's available
- Most themes have a default.hbs, however this file is a layout file, depended on by other templates, not a template file itself
- In production mode, using the default.hbs as a template causes weird, intermittent layout issues depending on which order pages are loaded
- This is due to this issue: TryGhost/express-hbs#161
- In Ghost v3, we will throw a 400 error for missing template files instead of having a fallback
- In the example above, navigating to '/' would throw the error 'Missing template home.hbs for route "/".'
@ErisDS
Copy link
Member Author

ErisDS commented Apr 9, 2020

New ref: https://forum.ghost.org/t/published-pages-result-in-500-error/13331

This has come up twice recently 🤔

@ErisDS
Copy link
Member Author

ErisDS commented Jun 30, 2020

Including the full (obscure) error that Ghost sees when this happens:

 InternalServerError: Cannot read property 'match' of undefined
        at new GhostError (/home/ghost/node_modules/@tryghost/errors/lib/errors.js:10:26)
        at _private.prepareError (/home/ghost/core/server/web/shared/middlewares/error-handler.js:53:19)
        at Layer.handle_error (/home/ghost/node_modules/express/lib/router/layer.js:71:5)
        at trim_prefix (/home/ghost/node_modules/express/lib/router/index.js:315:13)
        at /home/ghost/node_modules/express/lib/router/index.js:284:7
        at Function.process_params (/home/ghost/node_modules/express/lib/router/index.js:335:12)
        at next (/home/ghost/node_modules/express/lib/router/index.js:275:10)
        at handle404 (/home/ghost/core/frontend/apps/private-blogging/lib/middleware.js:166:20)
        at Layer.handle_error (/home/ghost/node_modules/express/lib/router/layer.js:71:5)
        at trim_prefix (/home/ghost/node_modules/express/lib/router/index.js:315:13)
        at /home/ghost/node_modules/express/lib/router/index.js:284:7
        at Function.process_params (/home/ghost/node_modules/express/lib/router/index.js:335:12)
        at next (/home/ghost/node_modules/express/lib/router/index.js:275:10)
        at Layer.handle_error (/home/ghost/node_modules/express/lib/router/layer.js:67:12)
        at trim_prefix (/home/ghost/node_modules/express/lib/router/index.js:315:13)
        at /home/ghost/node_modules/express/lib/router/index.js:284:7
        at Function.process_params (/home/ghost/node_modules/express/lib/router/index.js:335:12)
        at Immediate.next (/home/ghost/node_modules/express/lib/router/index.js:275:10)
        at Immediate._onImmediate (/home/ghost/node_modules/express/lib/router/index.js:635:15)
        at processImmediate (internal/timers.js:441:21)
        at process.topLevelDomainCallback (domain.js:131:23)

    TypeError: Cannot read property 'match' of undefined
        at ExpressHbs.declaredLayoutFile (/home/ghost/node_modules/express-hbs/lib/hbs.js:75:21)
        at parseLayout (/home/ghost/node_modules/express-hbs/lib/hbs.js:467:27)
        at /home/ghost/node_modules/express-hbs/lib/hbs.js:577:7
        at getSourceTemplate (/home/ghost/node_modules/express-hbs/lib/hbs.js:551:16)
        at compileFile (/home/ghost/node_modules/express-hbs/lib/hbs.js:573:5)
        at /home/ghost/node_modules/express-hbs/lib/hbs.js:660:12
        at ExpressHbs.loadDefaultLayout (/home/ghost/node_modules/express-hbs/lib/hbs.js:296:44)
        at ExpressHbs.___express (/home/ghost/node_modules/express-hbs/lib/hbs.js:648:8)
        at View.render (/home/ghost/node_modules/express/lib/view.js:135:8)
        at tryRender (/home/ghost/node_modules/express/lib/application.js:640:10)
        at Function.render (/home/ghost/node_modules/express/lib/application.js:592:3)
        at ServerResponse.render (/home/ghost/node_modules/express/lib/response.js:1012:7)
        at renderer (/home/ghost/core/frontend/services/routing/helpers/renderer.js:29:9)
        at renderEntry (/home/ghost/core/frontend/services/routing/helpers/render-entry.js:16:16)
        at then (/home/ghost/core/frontend/services/routing/controllers/entry.js:95:20)
        at bound (domain.js:420:14)
        at runBound (domain.js:433:12)
        at tryCatcher (/home/ghost/node_modules/bluebird/js/release/util.js:16:23)
        at Promise._settlePromiseFromHandler (/home/ghost/node_modules/bluebird/js/release/promise.js:547:31)
        at Promise._settlePromise (/home/ghost/node_modules/bluebird/js/release/promise.js:604:18)
        at Promise._settlePromise0 (/home/ghost/node_modules/bluebird/js/release/promise.js:649:10)
        at Promise._settlePromises (/home/ghost/node_modules/bluebird/js/release/promise.js:729:18)

@ErisDS ErisDS added the bug label Jun 30, 2020
ErisDS added a commit to ErisDS/Ghost that referenced this issue Nov 21, 2022
refs: TryGhost/Product#2289
refs: TryGhost/express-hbs#161

- Themes that resuse layouts as templates trigger horrible errors, which are thrown as 500s
- But there's nothing the server is doing wrong, it's a theme user, so we downgrade these to 400s
- There is more to do here to improve the errors shown, but this is just a first step to ensure that theme issues don't look like server failures
ErisDS added a commit to TryGhost/Ghost that referenced this issue Nov 22, 2022
refs: TryGhost/Product#2289
refs: TryGhost/express-hbs#161

- Themes that resuse layouts as templates trigger horrible errors, which are thrown as 500s
- But there's nothing the server is doing wrong, it's a theme user, so we downgrade these to 400s
- There is more to do here to improve the errors shown, but this is just a first step to ensure that theme issues don't look like server failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant