Skip to content

Commit

Permalink
fix(prerender): Allow overriding prerender parameter in a Route neste…
Browse files Browse the repository at this point in the history
…d in a Set (redwoodjs#6382)

Co-authored-by: Tobbe Lundberg <[email protected]>
  • Loading branch information
2 people authored and jtoar committed Sep 14, 2022
1 parent 36473ae commit 0330945
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
4 changes: 3 additions & 1 deletion packages/prerender/src/__tests__/detectRoutes.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { RWRoute } from '@redwoodjs/structure/dist/model/RWRoute'

import { detectPrerenderRoutes } from '../detection'

jest.mock('@redwoodjs/internal/dist/paths', () => {
Expand All @@ -14,7 +16,7 @@ jest.mock('@redwoodjs/internal/dist/paths', () => {

// Mock route detection, tested in @redwoodjs/structure separately

let mockedRoutes = []
let mockedRoutes: Partial<RWRoute>[] = []
jest.mock('@redwoodjs/structure', () => {
return {
getProject: jest.fn(() => {
Expand Down
29 changes: 20 additions & 9 deletions packages/structure/src/model/RWRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export class RWRoute extends BaseNode {
@lazy() get prerender(): boolean {
return this.getBoolAttr('prerender')
}

@lazy() get path_literal_node() {
const a = this.jsxNode.getAttribute('path')
if (!a) {
Expand Down Expand Up @@ -265,24 +266,34 @@ export class RWRoute extends BaseNode {
}

private getBoolAttr(name: string) {
const a = this.jsxNode.getAttribute(name)
const attr = this.jsxNode.getAttribute(name)
// No attribute
if (!a) {
if (!attr) {
return false
}

// Attribute exists
if (tsm.Node.isJsxAttribute(a)) {
const init = a.getInitializer()
// If it contains prerender="true"
if (tsm.Node.isStringLiteral(init!)) {
if (tsm.Node.isJsxAttribute(attr)) {
const init = attr.getInitializer()

// Bool attributes with no initializer are true
// e.g. <Route prerender />
if (!init) {
return true
}

if (tsm.Node.isJsxExpression(init)) {
// If it is explicitly set to true
// e.g. <Route prerender={true} />
return tsm.Node.isTrueLiteral(init.getExpression())
} else if (tsm.Node.isStringLiteral(init!)) {
// If its using the incorrect string form, we're accepting it as true
// e.g. <Route prerender="true" />
const literalValue = init.getLiteralValue()
return literalValue === 'true'
} else {
// If it contains just prerender
return true
}
}

return false
}

Expand Down
3 changes: 2 additions & 1 deletion packages/structure/src/model/RWRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export class RWRouter extends FileNode {
tsm.SyntaxKind.JsxSelfClosingElement
)) {
const tagName = x.getTagNameNode().getText()
if (tagName === 'Route') {
// Add prerender prop from <Set> if not already present
if (tagName === 'Route' && !x.getAttribute('prerender')) {
x.insertAttribute(0, { name: 'prerender' })
}
}
Expand Down

0 comments on commit 0330945

Please sign in to comment.