-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split out static props into their own function
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
1 parent
c5feb81
commit 4a2dc85
Showing
4 changed files
with
83 additions
and
46 deletions.
There are no files selected for viewing
67 changes: 22 additions & 45 deletions
67
packages/app-project/src/helpers/getDefaultPageProps/getDefaultPageProps.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/app-project/src/helpers/getStaticPageProps/getStaticPageProps.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './getStaticPageProps' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters