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

Vacms 14419 cms preview #162

Closed
wants to merge 4 commits into from
Closed
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: 2 additions & 2 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ NEXT_PUBLIC_DRUPAL_BASE_URL=https://content-build-medc0xjkxm4jmpzxl3tfbcs7qcddsi
NEXT_IMAGE_DOMAIN=https://content-build-medc0xjkxm4jmpzxl3tfbcs7qcddsivh.ci.cms.va.gov
DRUPAL_SITE_ID=
DRUPAL_PREVIEW_SECRET=
DRUPAL_CLIENT_ID=
DRUPAL_CLIENT_SECRET=
DRUPAL_CLIENT_ID=foo
DRUPAL_CLIENT_SECRET=bar
3 changes: 3 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const nextConfig = {
},
env: {
NEXT_PUBLIC_DRUPAL_BASE_URL: process.env.NEXT_PUBLIC_DRUPAL_BASE_URL,
DRUPAL_CLIENT_ID: process.env.DRUPAL_CLIENT_ID,
DRUPAL_CLIENT_SECRET: process.env.DRUPAL_CLIENT_SECRET,
DRUPAL_PREVIEW_SECRET: process.env.DRUPAL_PREVIEW_SECRET,
GOOGLE_TAG_MANAGER_AUTH: process.env.GOOGLE_TAG_MANAGER_AUTH,
GOOGLE_TAG_MANAGER_PREVIEW: process.env.GOOGLE_TAG_MANAGER_PREVIEW,
GOOGLE_TAG_MANAGER_ID: process.env.GOOGLE_TAG_MANAGER_ID,
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"scripts": {
"dev": "SSG=false next dev",
"build": "SSG=true next build",
"build:preview":"SSG=false next build",
"start": "next start",
"postbuild": "next-sitemap",
"export": "SSG=true next build && next export",
"export:serve": "http-server out -p 8001",
Expand Down
26 changes: 19 additions & 7 deletions src/data/queries/newsStory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { drupalClient } from '@/lib/utils/drupalClient'
import { queries } from '.'
import { NodeNewsStory } from '@/types/dataTypes/drupal/node'
import { NewsStoryType } from '@/types/index'
import { GetServerSidePropsContext } from 'next'

// Define the query params for fetching node--news_story.
export const params: QueryParams<null> = () => {
Expand All @@ -24,19 +25,30 @@ export const params: QueryParams<null> = () => {
// Define the option types for the data loader.
type DataOpts = QueryOpts<{
id: string
context?: GetServerSidePropsContext // from the preview api route
}>

// Implement the data loader.
export const data: QueryData<DataOpts, NodeNewsStory> = async (
opts
): Promise<NodeNewsStory> => {
const entity = await drupalClient.getResource<NodeNewsStory>(
'node--news_story',
opts?.id,
{
params: params().getQueryObject(),
}
)
const entity = opts.context.preview
? // need to use getResourceFromContext for unpublished revisions
await drupalClient.getResourceFromContext<NodeNewsStory>(
'node--news_story',
opts.context,
{
params: params().getQueryObject(),
}
)
: // otherwise just lookup by uuid
await drupalClient.getResource<NodeNewsStory>(
'node--news_story',
opts.id,
{
params: params().getQueryObject(),
}
)

return entity
}
Expand Down
6 changes: 6 additions & 0 deletions src/lib/utils/drupalClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,10 @@ export const fetcher = async (input: RequestInfo, init?: RequestInit) => {
export const drupalClient = new DrupalClient(baseUrl, {
fetcher,
useDefaultResourceTypeEntry: true,
auth: {
clientId: process.env.DRUPAL_CLIENT_ID,
clientSecret: process.env.DRUPAL_CLIENT_SECRET,
},
previewSecret: process.env.DRUPAL_PREVIEW_SECRET,
forceIframeSameSiteCookie: process.env.NODE_ENV === 'development',
})
16 changes: 12 additions & 4 deletions src/pages/[[...slug]].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
GetStaticPathsContext,
GetStaticPathsResult,
GetStaticPropsContext,
InferGetStaticPropsType,
} from 'next'
import Head from 'next/head'
import { QueryOpts } from 'next-drupal-query'
Expand All @@ -20,13 +21,14 @@ import {
getStaticPathsByResourceType,
} from '@/lib/utils/staticPaths'

export default function ResourcePage({ resource, globalElements }) {
export default function ResourcePage({ resource, preview, globalElements }) {
if (!resource) return null

const title = `${resource.title} | Veterans Affairs`

return (
<Wrapper bannerData={globalElements.bannerData}>
{preview ? <>This is a preview page</> : null}
<Head>
<title>{title}</title>
</Head>
Expand All @@ -46,7 +48,7 @@ export async function getStaticPaths(
// so we set SSG=true on `next build/export` and SSG=false on `next dev`.
// `getStaticPaths` will never be called during runtime (`next start`), but we could set
// SSG=false there as well, for good measure.
if (!process.env.SSG) {
if (process.env.SSG === 'false') {
return {
paths: [],
fallback: 'blocking',
Expand Down Expand Up @@ -74,7 +76,12 @@ export async function getStaticProps(context: GetStaticPropsContext) {
? drupalClient.getPathFromContext(context)
: isListingPage.path

const pathInfo = await drupalClient.translatePath(path)
// need to use Context here for previewing unpublished revisions
const pathInfo =
isListingPage === false
? await drupalClient.translatePathFromContext(context)
: await drupalClient.translatePath(path)

if (!pathInfo) {
return {
notFound: true,
Expand Down Expand Up @@ -113,14 +120,15 @@ export async function getStaticProps(context: GetStaticPropsContext) {

// If we're not in preview mode and the resource is not published,
// Return page not found.
if (!context.preview && resource?.published === false) {
if (!context.preview && !resource?.published) {
return {
notFound: true,
}
}

return {
props: {
preview: context.preview || false,
resource,
globalElements: await getGlobalElements(
pathInfo.jsonapi?.entryPoint,
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/exit-preview.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextApiResponse } from 'next'

export default async function exit(_, response: NextApiResponse) {
export default function exit(_, response: NextApiResponse) {
response.clearPreviewData()
response.writeHead(307, { Location: '/' })
response.end()
Expand Down
10 changes: 8 additions & 2 deletions src/pages/api/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { DrupalPreview } from 'next-drupal'
import { NextApiRequest, NextApiResponse } from 'next'
import { drupalClient } from '@/lib/utils/drupalClient'

export default DrupalPreview()
export default async function handler(
request: NextApiRequest,
response: NextApiResponse
) {
await drupalClient.preview(request, response)
}