Skip to content

Commit

Permalink
Split out static props into their own function
Browse files Browse the repository at this point in the history
Perform all data fetching in getStaticPageProps (props that only depend on the page URL.) Return entry with a 404 to avoid extra API calls when we know there's nothing to fetch.
  • Loading branch information
eatyourgreens committed Jan 25, 2021
1 parent c5feb81 commit 4a2dc85
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,67 +1,44 @@
import getCookie from '@helpers/getCookie'
import fetchWorkflowsHelper from '@helpers/fetchWorkflowsHelper'
import initStore from '@stores'
import { getSnapshot } from 'mobx-state-tree'
import getStaticPageProps from '@helpers/getStaticPageProps'

export default async function getDefaultPageProps({ params, query, req, res }) {

// cookie is in the next.js context req object
const mode = getCookie(req, 'mode') || null
const dismissedAnnouncementBanner = getCookie(req, 'dismissedAnnouncementBanner') || null
const snapshot = {
ui: {
dismissedAnnouncementBanner,
mode
}
}
const isServer = true
const store = initStore(isServer, snapshot)

if (params.owner && params.project) {
const { owner, project } = params
const projectSlug = `${owner}/${project}`
const { env } = query
await store.project.fetch(projectSlug, { env })
if (!store.project.id) {
res.statusCode = 404
return {
props: {
statusCode: 404,
title: `Project ${owner}/${project} was not found.`
}
}
}
}

const workflowExists = store.project.links.active_workflows.includes(params.workflowID)
if (params.workflowID && !workflowExists) {
res.statusCode = 404
const { props: staticProps } = await getStaticPageProps({ params, query })
const { project, isServer, statusCode, title, workflowID, workflows } = staticProps
if (statusCode) {
res.statusCode = statusCode
return {
props: {
statusCode: 404,
title: `Workflow ${params.workflowID} was not found.`
statusCode,
title
}
}
}

const { project, ui } = getSnapshot(store)
const { headers, connection } = req
const { env } = query
const language = project.primary_language
const { active_workflows, default_workflow } = project.links
const workflows = await fetchWorkflowsHelper(language, active_workflows, default_workflow, env)
const host = generateHostUrl(headers, connection)
/*
snapshot for store hydration in the browser
*/
const initialState = {
project,
ui: {
dismissedAnnouncementBanner,
mode
}
}

const props = {
host: generateHostUrl(headers, connection),
initialState: {
project,
ui
},
isServer: true,
host,
initialState,
isServer,
query,
workflows
}

const workflowID = store.project.defaultWorkflow
if (workflowID) {
props.workflowID = workflowID
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import fetchWorkflowsHelper from '@helpers/fetchWorkflowsHelper'
import initStore from '@stores'
import { getSnapshot } from 'mobx-state-tree'

export default async function getStaticPageProps({ params, query }) {
const isServer = true
const store = initStore(isServer)
const { env } = query

/*
Fetch the project
*/
if (params.owner && params.project) {
const { owner, project } = params
const projectSlug = `${owner}/${project}`
await store.project.fetch(projectSlug, { env })
if (!store.project.id) {
return {
props: {
statusCode: 404,
title: `Project ${owner}/${project} was not found.`
}
}
}
}

/*
Validate any workflow URLs
*/
const { project } = getSnapshot(store)
const language = project.primary_language
const { active_workflows, default_workflow } = project.links
const workflowExists = active_workflows.includes(params.workflowID)
if (params.workflowID && !workflowExists) {
return {
props: {
statusCode: 404,
title: `Workflow ${params.workflowID} was not found.`
}
}
}

/*
Fetch the active project workflows
*/
const workflows = await fetchWorkflowsHelper(language, active_workflows, default_workflow, env)
const props = {
project,
isServer,
workflows
}

const workflowID = store.project.defaultWorkflow
if (workflowID) {
props.workflowID = workflowID
}

return { props }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './getStaticPageProps'
2 changes: 1 addition & 1 deletion packages/app-project/stores/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const Project = types
get defaultWorkflow() {
const activeWorkflows = self.links['active_workflows']
let singleActiveWorkflow
if (activeWorkflows.length === 1) {
if (activeWorkflows?.length === 1) {
[singleActiveWorkflow] = self.links['active_workflows']
}
return singleActiveWorkflow
Expand Down

0 comments on commit 4a2dc85

Please sign in to comment.