-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support self targeting in app router (#974)
- Loading branch information
Showing
12 changed files
with
246 additions
and
32 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
e2e/nextjs/app-router/app/app/self-targeting/ClientComponent.tsx
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,30 @@ | ||
'use client' | ||
import { | ||
useVariableValue, | ||
useAllVariables, | ||
useAllFeatures, | ||
renderIfEnabled, | ||
} from '@devcycle/nextjs-sdk' | ||
|
||
const ConditionalComponent = renderIfEnabled( | ||
'enabled-feature', | ||
() => import('./ConditionalClientComponent'), | ||
) | ||
|
||
export const ClientComponent = () => { | ||
const enabledVar = useVariableValue('enabled-feature', false) | ||
const disabledVar = useVariableValue('disabled-feature', false) | ||
const allVariables = useAllVariables() | ||
const allFeatures = useAllFeatures() | ||
|
||
return ( | ||
<div> | ||
<h1>Client Component</h1> | ||
<p>Client Enabled Variable: {JSON.stringify(enabledVar)}</p> | ||
<p>Client Disabled Variable: {JSON.stringify(disabledVar)}</p> | ||
<p>Client All Variables: {JSON.stringify(allVariables)}</p> | ||
<p>Client All Features: {JSON.stringify(allFeatures)}</p> | ||
<ConditionalComponent /> | ||
</div> | ||
) | ||
} |
9 changes: 9 additions & 0 deletions
9
e2e/nextjs/app-router/app/app/self-targeting/ConditionalClientComponent.tsx
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,9 @@ | ||
'use client' | ||
|
||
export default function ConditionalClientComponent() { | ||
return ( | ||
<div> | ||
<h1>Client Component Conditionally Bundled</h1> | ||
</div> | ||
) | ||
} |
17 changes: 17 additions & 0 deletions
17
e2e/nextjs/app-router/app/app/self-targeting/ServerComponent.tsx
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,17 @@ | ||
import { getAllFeatures, getAllVariables, getVariableValue } from './devcycle' | ||
export const ServerComponent = async () => { | ||
const enabledVar = await getVariableValue('enabled-feature', false) | ||
const disabledVar = await getVariableValue('disabled-feature', false) | ||
const allVariables = await getAllVariables() | ||
const allFeatures = await getAllFeatures() | ||
|
||
return ( | ||
<div> | ||
<h1>Server Component</h1> | ||
<p>Server Enabled Variable: {JSON.stringify(enabledVar)}</p> | ||
<p>Server Disabled Variable: {JSON.stringify(disabledVar)}</p> | ||
<p>Server All Variables: {JSON.stringify(allVariables)}</p> | ||
<p>Server All Features: {JSON.stringify(allFeatures)}</p> | ||
</div> | ||
) | ||
} |
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,22 @@ | ||
import { setupDevCycle } from '@devcycle/nextjs-sdk/server' | ||
import { headers } from 'next/headers' | ||
export const { | ||
getVariableValue, | ||
getClientContext, | ||
getAllVariables, | ||
getAllFeatures, | ||
} = setupDevCycle({ | ||
clientSDKKey: process.env.NEXT_PUBLIC_E2E_NEXTJS_CLIENT_KEY ?? '', | ||
serverSDKKey: process.env.E2E_NEXTJS_SERVER_KEY ?? '', | ||
userGetter: async () => { | ||
const reqHeaders = headers() | ||
return { | ||
user_id: 'self-targeting-user', | ||
customData: { | ||
// set a dummy field here so that the headers call stays in the build output | ||
someKey: reqHeaders.get('some-key'), | ||
}, | ||
} | ||
}, | ||
options: { enableStreaming: false }, | ||
}) |
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,25 @@ | ||
import type { Metadata } from 'next' | ||
import React from 'react' | ||
import { DevCycleClientsideProvider } from '@devcycle/nextjs-sdk' | ||
import { getClientContext } from './devcycle' | ||
|
||
export const metadata: Metadata = { | ||
title: 'Create Next App', | ||
description: 'Generated by create next app', | ||
} | ||
|
||
export default async function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}) { | ||
return ( | ||
<html lang="en"> | ||
<body> | ||
<DevCycleClientsideProvider context={getClientContext()}> | ||
{children} | ||
</DevCycleClientsideProvider> | ||
</body> | ||
</html> | ||
) | ||
} |
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,19 @@ | ||
import { ClientComponent } from './ClientComponent' | ||
import { ServerComponent } from './ServerComponent' | ||
import React, { Suspense } from 'react' | ||
import Link from 'next/link' | ||
|
||
const Home = async () => { | ||
return ( | ||
<main> | ||
<div>Streaming Disabled</div> | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<ClientComponent /> | ||
<ServerComponent /> | ||
</Suspense> | ||
<Link href="/normal/test">Go To page</Link> | ||
</main> | ||
) | ||
} | ||
|
||
export default Home |
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
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
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
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
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
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