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

Fetch resources faster #1798

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 0 additions & 3 deletions src/components/Learningpath/Learningpath.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ const Learningpath = ({
)}
<LearningpathEmbed
skipToContentId={!learningpathStep.showTitle ? skipToContentId : undefined}
topic={topic}
subjectId={subject?.id}
learningpathStep={learningpathStep}
breadcrumbItems={breadcrumbItems}
Expand Down Expand Up @@ -227,9 +226,7 @@ Learningpath.fragments = {
topic: gql`
fragment Learningpath_Topic on Topic {
...LastLearningpathStepInfo_Topic
...LearningpathEmbed_Topic
}
${LearningpathEmbed.fragments.topic}
${LastLearningpathStepInfo.fragments.topic}
`,
resourceType: gql`
Expand Down
14 changes: 3 additions & 11 deletions src/components/Learningpath/LearningpathEmbed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import config from "../../config";
import ErrorPage from "../../containers/ErrorPage";
import {
GQLLearningpathEmbed_LearningpathStepFragment,
GQLLearningpathEmbed_TopicFragment,
GQLLearningpathStepQuery,
GQLLearningpathStepQueryVariables,
} from "../../graphqlTypes";
Expand Down Expand Up @@ -72,12 +71,11 @@ const converterComponents: DynamicComponents = {

interface Props {
learningpathStep: GQLLearningpathEmbed_LearningpathStepFragment;
topic?: GQLLearningpathEmbed_TopicFragment;
skipToContentId?: string;
breadcrumbItems: Breadcrumb[];
subjectId?: string;
}
const LearningpathEmbed = ({ learningpathStep, skipToContentId, topic, subjectId, breadcrumbItems }: Props) => {
const LearningpathEmbed = ({ learningpathStep, skipToContentId, subjectId, breadcrumbItems }: Props) => {
const { t, i18n } = useTranslation();
const location = useLocation();
const [taxId, articleId] =
Expand Down Expand Up @@ -178,7 +176,7 @@ const LearningpathEmbed = ({ learningpathStep, skipToContentId, topic, subjectId
id={skipToContentId}
article={article}
oembed={data?.article?.oembed}
{...getArticleProps(resource, topic)}
{...getArticleProps(resource)}
>
{path ? <CreatedBy name={t("createdBy.content")} description={t("createdBy.text")} url={contentUrl} /> : <></>}
</Article>
Expand Down Expand Up @@ -206,13 +204,6 @@ const articleFragment = gql`
`;

LearningpathEmbed.fragments = {
topic: gql`
fragment LearningpathEmbed_Topic on Topic {
supplementaryResources(subjectId: $subjectId) {
id
}
}
`,
article: articleFragment,
learningpathStep: gql`
fragment LearningpathEmbed_LearningpathStep on LearningpathStep {
Expand Down Expand Up @@ -254,6 +245,7 @@ const learningpathStepQuery = gql`
resource(id: $resourceId) @include(if: $includeResource) {
id
path
relevanceId
resourceTypes {
id
name
Expand Down
31 changes: 10 additions & 21 deletions src/containers/ArticlePage/ArticlePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ const ArticlePage = ({
const { user, authContextLoaded } = useContext(AuthContext);
const { t, i18n } = useTranslation();
const { trackPageView } = useTracker();
const subjectPageUrl = config.ndlaFrontendDomain;

useEffect(() => {
if (!loading && authContextLoaded) {
Expand Down Expand Up @@ -135,32 +134,24 @@ const ArticlePage = ({
const contentType = resource ? getContentType(resource) : undefined;
const resourceType = contentType && isHeroContentType(contentType) ? contentType : undefined;

const copyPageUrlLink = topic ? `${subjectPageUrl}${topic.path}/${resource.id.replace("urn:", "")}` : undefined;
const printUrl = `${subjectPageUrl}/article-iframe/${i18n.language}/article/${resource.article.id}`;
const printUrl = `${config.ndlaFrontendDomain}/article-iframe/${i18n.language}/article/${resource.article.id}`;

const breadcrumbItems = toBreadcrumbItems(t("breadcrumb.toFrontpage"), [...topicPath, resource]);

return (
<main>
<ArticleHero
subject={subject}
resourceType={resourceType}
metaImage={article.metaImage}
breadcrumbItems={breadcrumbItems}
/>
<ArticleHero resourceType={resourceType} metaImage={article.metaImage} breadcrumbItems={breadcrumbItems} />
<Helmet>
<title>{`${getDocumentTitle(t, resource, subject)}`}</title>
{scripts?.map((script) => (
<script key={script.src} src={script.src} type={script.type} async={script.async} defer={script.defer} />
))}
{copyPageUrlLink && (
<link
rel="alternate"
type="application/json+oembed"
href={`${config.ndlaFrontendDomain}/oembed?url=${copyPageUrlLink}`}
title={article.title}
/>
)}
<link
rel="alternate"
type="application/json+oembed"
href={`${config.ndlaFrontendDomain}/oembed?url=${config.ndlaFrontendDomain}${resource.path}`}
title={article.title}
/>
{subject?.metadata.customFields?.[TAXONOMY_CUSTOM_FIELD_SUBJECT_CATEGORY] ===
constants.subjectCategories.ARCHIVE_SUBJECTS && <meta name="robots" content="noindex, nofollow" />}
<meta name="pageid" content={`${article.id}`} />
Expand All @@ -186,7 +177,7 @@ const ArticlePage = ({
subjectId={subject?.id}
showFavoriteButton={config.feideEnabled}
oembed={article.oembed}
{...getArticleProps(resource, topic)}
{...getArticleProps(resource)}
/>
{topic && (
<LayoutItem layout="extend">
Expand Down Expand Up @@ -226,16 +217,15 @@ export const articlePageFragments = {
title
}
}
...ArticleHero_Subject
}
${ArticleHero.fragments.subject}
`,
resource: gql`
fragment ArticlePage_Resource on Resource {
id
name
path
contentUri
relevanceId
article {
created
updated
Expand All @@ -255,7 +245,6 @@ export const articlePageFragments = {
`,
topic: gql`
fragment ArticlePage_Topic on Topic {
path
...Resources_Topic
}
${Resources.fragments.topic}
Expand Down
12 changes: 3 additions & 9 deletions src/containers/ArticlePage/components/ArticleHero.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { gql } from "@apollo/client";
import styled from "@emotion/styled";
import { stackOrder } from "@ndla/core";
import { Hero, HeroContent, HeroContentType, HomeBreadcrumb, NdlaFilmHero, OneColumn } from "@ndla/ui";
import { GQLArticleHero_MetaImageFragment, GQLArticleHero_SubjectFragment } from "../../../graphqlTypes";
import { GQLArticleHero_MetaImageFragment } from "../../../graphqlTypes";
import { Breadcrumb as BreadcrumbType } from "../../../interfaces";
import { useIsNdlaFilm } from "../../../routeHelpers";

Expand Down Expand Up @@ -52,7 +52,6 @@ const HeroBackground = styled.div`
`;

interface Props {
subject?: GQLArticleHero_SubjectFragment;
resourceType?: HeroContentType;
metaImage?: GQLArticleHero_MetaImageFragment;
breadcrumbItems: BreadcrumbType[];
Expand All @@ -62,7 +61,7 @@ const StyledSection = styled.section`
z-index: ${stackOrder.offsetSingle};
`;

const ArticleHero = ({ resourceType, metaImage, subject, breadcrumbItems }: Props) => {
const ArticleHero = ({ resourceType, metaImage, breadcrumbItems }: Props) => {
const ndlaFilm = useIsNdlaFilm();
return (
<WrapperComponent ndlaFilm={ndlaFilm} resourceType={resourceType} metaImage={metaImage}>
Expand All @@ -74,7 +73,7 @@ const ArticleHero = ({ resourceType, metaImage, subject, breadcrumbItems }: Prop
<OneColumn>
<HeroContent data-image={!!(ndlaFilm && metaImage?.url)}>
<StyledSection>
{subject && <HomeBreadcrumb light={ndlaFilm ? true : undefined} items={breadcrumbItems} />}
{<HomeBreadcrumb light={ndlaFilm ? true : undefined} items={breadcrumbItems} />}
</StyledSection>
</HeroContent>
</OneColumn>
Expand All @@ -83,11 +82,6 @@ const ArticleHero = ({ resourceType, metaImage, subject, breadcrumbItems }: Prop
};

ArticleHero.fragments = {
subject: gql`
fragment ArticleHero_Subject on Subject {
id
}
`,
metaImage: gql`
fragment ArticleHero_MetaImage on MetaImage {
url
Expand Down
10 changes: 1 addition & 9 deletions src/containers/PlainArticlePage/PlainArticleContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import AddEmbedToFolder from "../../components/MyNdla/AddEmbedToFolder";
import SocialMediaMetadata from "../../components/SocialMediaMetadata";
import config from "../../config";
import { GQLPlainArticleContainer_ArticleFragment } from "../../graphqlTypes";
import { getArticleProps } from "../../util/getArticleProps";
import { getArticleScripts } from "../../util/getArticleScripts";
import getStructuredDataFromArticle, { structuredArticleDataFragment } from "../../util/getStructuredDataFromArticle";
import { htmlTitle } from "../../util/titleHelper";
Expand Down Expand Up @@ -97,14 +96,7 @@ const PlainArticleContainer = ({ article: propArticle, skipToContentId }: Props)
trackableContent={article}
/>
<OneColumn>
<Article
contentTransformed
isPlainArticle
id={skipToContentId}
article={article}
oembed={undefined}
{...getArticleProps(undefined, undefined)}
/>
<Article contentTransformed isPlainArticle id={skipToContentId} article={article} oembed={undefined} label="" />
</OneColumn>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
GQLFolderResourceMetaSearchQuery,
GQLSharedResourceArticleContainer_ArticleFragment,
} from "../../../graphqlTypes";
import { getArticleProps } from "../../../util/getArticleProps";
import { getArticleScripts } from "../../../util/getArticleScripts";
import { getContentTypeFromResourceTypes } from "../../../util/getContentType";
import { structuredArticleDataFragment } from "../../../util/getStructuredDataFromArticle";
Expand Down Expand Up @@ -97,7 +96,6 @@ const SharedArticleContainer = ({ article: propArticle, meta, title }: Props) =>
id={SKIP_TO_CONTENT_ID}
oembed={undefined}
article={article}
{...getArticleProps(undefined, undefined)}
contentType={contentType?.contentType}
label={contentType?.label || ""}
/>
Expand Down
11 changes: 4 additions & 7 deletions src/graphqlTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2221,8 +2221,7 @@ export type GQLLastLearningpathStepInfo_ResourceTypeDefinitionFragment = {
__typename?: "ResourceTypeDefinition";
} & GQLResources_ResourceTypeDefinitionFragment;

export type GQLLearningpath_TopicFragment = { __typename?: "Topic" } & GQLLastLearningpathStepInfo_TopicFragment &
GQLLearningpathEmbed_TopicFragment;
export type GQLLearningpath_TopicFragment = { __typename?: "Topic" } & GQLLastLearningpathStepInfo_TopicFragment;

export type GQLLearningpath_ResourceTypeDefinitionFragment = {
__typename?: "ResourceTypeDefinition";
Expand Down Expand Up @@ -2277,11 +2276,6 @@ export type GQLLearningpathEmbed_ArticleFragment = {
} & GQLStructuredArticleDataFragment &
GQLArticle_ArticleFragment;

export type GQLLearningpathEmbed_TopicFragment = {
__typename?: "Topic";
supplementaryResources?: Array<{ __typename?: "Resource"; id: string }>;
};

export type GQLLearningpathEmbed_LearningpathStepFragment = {
__typename?: "LearningpathStep";
resource?: {
Expand Down Expand Up @@ -2309,6 +2303,7 @@ export type GQLLearningpathStepQuery = {
__typename?: "Resource";
id: string;
path: string;
relevanceId?: string;
resourceTypes?: Array<{ __typename?: "ResourceType"; id: string; name: string }>;
};
};
Expand Down Expand Up @@ -2502,6 +2497,7 @@ export type GQLArticlePage_ResourceFragment = {
name: string;
path: string;
contentUri?: string;
relevanceId?: string;
article?: {
__typename?: "Article";
created: string;
Expand Down Expand Up @@ -4293,6 +4289,7 @@ export type GQLIframeArticlePage_ResourceFragment = {
__typename?: "Resource";
id: string;
path: string;
relevanceId?: string;
resourceTypes?: Array<{ __typename?: "ResourceType"; id: string; name: string }>;
};

Expand Down
1 change: 1 addition & 0 deletions src/iframe/IframeArticlePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export const iframeArticlePageFragments = {
fragment IframeArticlePage_Resource on Resource {
id
path
relevanceId
resourceTypes {
id
name
Expand Down
10 changes: 2 additions & 8 deletions src/util/getArticleProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,12 @@
import { getContentType } from "./getContentType";
import { GQLResource } from "../graphqlTypes";

interface Topic {
supplementaryResources?: { id: string }[];
}
export const getArticleProps = <T extends Topic>(
resource: Pick<GQLResource, "resourceTypes" | "id"> | undefined,
topic?: T,
) => {
export const getArticleProps = (resource: Pick<GQLResource, "resourceTypes" | "id" | "relevanceId"> | undefined) => {
const hasResourceTypes = resource?.resourceTypes && resource?.resourceTypes?.length > 0;

const contentType = hasResourceTypes && resource ? getContentType(resource) : undefined;

const additional = topic?.supplementaryResources?.some((item) => item?.id === resource?.id) ?? false;
const additional = resource?.relevanceId !== "urn:relevance:core";

const label = (hasResourceTypes && resource?.resourceTypes![0]?.name) || "";
return { contentType, label, additional };
Expand Down
Loading