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

add base edge code #419

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Currently supports the following templates engines:
- [`doT`](https://github.com/olado/doT)
- [`eta`](https://eta.js.org)

- [`edge`](https://edgejs.dev)

This branch is a wip adding https://edgejs.dev/

In `production` mode, `@fastify/view` will heavily cache the templates file and functions, while in `development` will reload every time the template file and function.

_Note: For **Fastify v3 support**, please use point-of-view `5.x` (npm i point-of-view@5)._
Expand Down
39 changes: 37 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fp = require('fastify-plugin')
const { accessSync, existsSync, mkdirSync, readdirSync } = require('node:fs')
const { basename, dirname, extname, join, resolve } = require('node:path')
const { LruMap } = require('toad-cache')
const supportedEngines = ['ejs', 'nunjucks', 'pug', 'handlebars', 'mustache', 'art-template', 'twig', 'liquid', 'dot', 'eta']
const supportedEngines = ['ejs', 'nunjucks', 'pug', 'handlebars', 'mustache', 'art-template', 'twig', 'liquid', 'dot', 'eta', 'edge']

const viewCache = Symbol('@fastify/view/cache')

Expand Down Expand Up @@ -62,7 +62,7 @@ async function fastifyView (fastify, opts) {
}

function layoutIsValid (_layoutFileName) {
if (type !== 'dot' && type !== 'handlebars' && type !== 'ejs' && type !== 'eta') {
if (type !== 'dot' && type !== 'handlebars' && type !== 'ejs' && type !== 'eta' && type !== 'edge') {
throw new Error('Only Dot, Handlebars, EJS, and Eta support the "layout" option')
}

Expand Down Expand Up @@ -101,6 +101,9 @@ async function fastifyView (fastify, opts) {
liquid: viewLiquid,
dot: withLayout(dotRender, globalLayoutFileName),
eta: withLayout(viewEta, globalLayoutFileName),
edge: withLayout(viewEdge, globalLayoutFileName),
// edge:(viewEdge),

_default: view
}

Expand Down Expand Up @@ -614,6 +617,38 @@ async function fastifyView (fastify, opts) {
}
}

// edge
async function viewEdge (page, data, opts) {
if (opts?.layout) {
layoutIsValid(opts.layout)
return withLayout(viewEdge, opts.layout).call(this, page, data)
}
data = Object.assign({}, defaultCtx, this.locals, data)
if (typeof page === 'function') {
return page(data)
}
let isRaw = false
if (typeof page === 'object' && page.raw) {
isRaw = true
page = page.raw.toString()
} else {
// append view extension
page = getPage(page, type)
}
const toHtml = fastify[viewCache].get(page)

if (toHtml && prod) {
return toHtml(data)
} else if (isRaw) {
const compiledPage = readCallbackParser(page, page, opts)
return compiledPage(data)
}

const file = await readFileSemaphore(join(templatesDir, page))
const render = readCallback(page, data, opts, file)
return render(data)
}

if (prod && type === 'handlebars' && globalOptions.partials) {
const partialsObject = await getPartials(type, { partials: globalOptions.partials, requestedPath: getRequestedPath(this) })
Object.keys(partialsObject).forEach((name) => {
Expand Down
6 changes: 6 additions & 0 deletions notes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
git checkout -b edge_branch

git add .
git commit -m "core code to add edge"
git push --set-upstream origin edge_branch

Loading