Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Migrate pages from Page.getInitialProps to getServerSideProps #2957

Merged
merged 10 commits into from Feb 15, 2022

Conversation

edmundito
Copy link
Contributor

@edmundito edmundito commented Feb 10, 2022

Change description

This updates all UI pages to use getServerSideProps over getInitialProps as it's the newest recommended way to use Next: https://nextjs.org/docs/api-reference/data-fetching/get-initial-props

// Before:

export default function Page (props) {
  return { ... }
}

Page.getInitialProps = (ctx: NextPageContext) => {
}


// Now:

export const getServerSideProps = withServerErrorHandler((ctx) => {
  return { props: { ... } }
})

const Page: NextPageWithInferredProps<typeof getServerSideProps> = (props) => {
  // Now all the props are typed
}

export default Page

The only caveat is that you have to ensure that getServerSideProps is exported on the apps that consume ui-components:

// Before:
import { default } from '@ui-components/pages/example'

// Now:
import { default, getServerSideProps } from '@ui-components/pages/example'

The only exceptions are _app.tsx and _error.tsx. These still use getInitialProps because getServerSideProps is not supported as of now:
https://nextjs.org/docs/advanced-features/custom-app#caveats
https://nextjs.org/docs/advanced-features/custom-error-page#caveats

It also ensures that all the props coming from getServerSideProps are typed.

Also fixes a few typing issues along the way as the files were being migrated, most notably the groups/rules.tsx page.

Checklists

Development

  • Application changes have been tested appropriately

Impact

  • Code follows company security practices and guidelines
  • Security impact of change has been considered
  • Performance impact of change has been considered
  • Possible migration needs considered (model migrations, config migrations, etc.)

Please explain any security, performance, migration, or other impacts if relevant:

Code review

  • Pull request has a descriptive title and context useful to a reviewer. Screenshots or screencasts are attached where applicable.
  • Relevant tags have been added to the PR (bug, enhancement, internal, etc.)

@edmundito edmundito added the internal chores and housekeeping label Feb 10, 2022
Copy link
Member

@evantahler evantahler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! This kind of consistency and better types will hopefully make it easier to avoid bugs, and for folks to know the "right way" to do things

@edmundito edmundito force-pushed the 181142452-migrate-to-server-side-props branch from e2d5c95 to 1ec1c8a Compare February 10, 2022 18:56
Copy link
Contributor

@krishnaglick krishnaglick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there could be some speed efficiencies by combining async requests in the getServerSideProps call, but that might be worth ticketing versus doing that work here.

ui/ui-community/pages/imports/[creatorId].tsx Show resolved Hide resolved
ui/ui-components/components/export/List.tsx Show resolved Hide resolved
appContext.query;
const { groups } = await client.request("get", `/groups`);
const { id, limit, offset, state, recordId, destinationId } = ctx.query;
const { groups } = await client.request<Actions.GroupsList>("get", `/groups`);

let exportProcessorId: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, though I know this was around before:

Suggested change
let exportProcessorId: string;
let exportProcessorId = "";

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will leave as is because it was like that before. I'm not sure if this will cause the action to fail later on, and I'd like not to specifically test this.

@@ -428,7 +428,7 @@ RecordsList.hydrate = async (
caseSensitive,
} = ctx.query;

const { records, total }: Actions.RecordsList = await client.request(
const { records, total } = await client.request<Actions.RecordsList>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +43 to +51
let scheduleRuns = [];
let schedules = [];

if (sources) {
schedules = sources
.filter(
(source) => source.appId === app.id && source.schedule?.refreshEnabled
)
.map((source) => source.schedule);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, and I'm not sure if this is more efficient.

Suggested change
let scheduleRuns = [];
let schedules = [];
if (sources) {
schedules = sources
.filter(
(source) => source.appId === app.id && source.schedule?.refreshEnabled
)
.map((source) => source.schedule);
const scheduleRuns = [];
const schedules = [];
if (sources) {
schedules.push(...sources
.filter(
(source) => source.appId === app.id && source.schedule?.refreshEnabled
)
.map((source) => source.schedule));

ui/ui-components/pages/oauth/callback.tsx Show resolved Hide resolved

plugins = plugins
.filter((p) => p.apps?.length > 0)
.sort((a, b) => (a.name > b.name ? 1 : -1));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.sort((a, b) => (a.name > b.name ? 1 : -1));
.sort((a, b) => (a.name > b.name ? 1 : a.name < b.name ? -1 : 0));

);
ensureMatchingModel("Source", source.modelId, modelId.toString());

const runsListInitialProps = await RunsList.hydrate(ctx, { topic: "source" });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's probably worth putting what RunsList.hydrate does into another method and calling that method from both places, rather than doing it like it.

@edmundito
Copy link
Contributor Author

@krishnaglick I appreciate all the suggestions but I wanted to focus this on migrating from one function to another without making a lot of changes unless it was necessary (like that groups rules file). I rather not apply these changes since every I apply I should re-test.

@krishnaglick
Copy link
Contributor

@krishnaglick I appreciate all the suggestions but I wanted to focus this on migrating from one function to another without making a lot of changes unless it was necessary (like that groups rules file). I rather not apply these changes since every I apply I should re-test.

I feel like these two should be addressed:
Commented Code
Incorrect Sorting

Everything else can be ticketed or counted as a nitpick I'd say.

@edmundito edmundito force-pushed the 181142452-migrate-to-server-side-props branch from ff71fa3 to 90fef65 Compare February 14, 2022 14:13
@edmundito edmundito force-pushed the 181142452-migrate-to-server-side-props branch from 90fef65 to f83bccd Compare February 15, 2022 18:00
@edmundito
Copy link
Contributor Author

edmundito commented Feb 15, 2022

Rebased and address @krishnaglick's comments.

@edmundito edmundito merged commit f48b4f0 into main Feb 15, 2022
@edmundito edmundito deleted the 181142452-migrate-to-server-side-props branch February 15, 2022 19:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
internal chores and housekeeping
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants