Skip to content

Commit 3eca1f6

Browse files
authored
Merge pull request #111 from strapi/fix/caching-on-development
fix: caching on development
2 parents 8de202d + 3d1c343 commit 3eca1f6

File tree

4 files changed

+181
-33
lines changed

4 files changed

+181
-33
lines changed

next/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
WEBSITE_URL=http://localhost:3000 # Add the correct ENV var for this onto your hosting platform, point it to your production website.
2+
ENVIRONMENT=development
23
PORT=3000
34

45
NEXT_PUBLIC_API_URL=http://localhost:1337

next/lib/strapi/client.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ export async function fetchCollectionType<T = API.Document[]>(
7676
return data as T;
7777
}
7878

79+
// Bypass cache in development mode
80+
if (process.env.ENVIRONMENT === 'development') {
81+
const { data } = await createClient(config)
82+
.collection(collectionName)
83+
.find({
84+
...options,
85+
status: 'published',
86+
});
87+
return data as T;
88+
}
89+
7990
// Use cached version for published content
8091
return fetchCollectionCached<T>(collectionName, options, config);
8192
} catch (error) {
@@ -133,6 +144,16 @@ export async function fetchSingleType<T = API.Document>(
133144
return data as T;
134145
}
135146

147+
if (process.env.ENVIRONMENT === 'development') {
148+
const { data } = await createClient(config)
149+
.single(singleTypeName)
150+
.find({
151+
...options,
152+
status: 'published',
153+
});
154+
return data as T;
155+
}
156+
136157
return fetchSingleCached<T>(singleTypeName, options, config);
137158
} catch (error) {
138159
throw new StrapiError(
@@ -191,6 +212,16 @@ export async function fetchDocument<T = API.Document>(
191212
return data as T;
192213
}
193214

215+
if (process.env.ENVIRONMENT === 'development') {
216+
const { data } = await createClient(config)
217+
.collection(collectionName)
218+
.findOne(documentId, {
219+
...options,
220+
status: 'published',
221+
});
222+
return data as T;
223+
}
224+
194225
return fetchDocumentCached<T>(collectionName, documentId, options, config);
195226
} catch (error) {
196227
throw new StrapiError(

strapi/src/api/plan/content-types/plan/schema.json

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,74 @@
1010
"options": {
1111
"draftAndPublish": true
1212
},
13-
"pluginOptions": {},
13+
"pluginOptions": {
14+
"i18n": {
15+
"localized": true
16+
}
17+
},
1418
"attributes": {
1519
"name": {
16-
"type": "string"
20+
"type": "string",
21+
"pluginOptions": {
22+
"i18n": {
23+
"localized": true
24+
}
25+
}
1726
},
1827
"price": {
19-
"type": "integer"
28+
"type": "integer",
29+
"pluginOptions": {
30+
"i18n": {
31+
"localized": true
32+
}
33+
}
2034
},
2135
"sub_text": {
22-
"type": "string"
36+
"type": "string",
37+
"pluginOptions": {
38+
"i18n": {
39+
"localized": true
40+
}
41+
}
2342
},
2443
"featured": {
2544
"type": "boolean",
45+
"pluginOptions": {
46+
"i18n": {
47+
"localized": true
48+
}
49+
},
2650
"default": false
2751
},
2852
"CTA": {
29-
"displayName": "CTA",
3053
"type": "component",
31-
"repeatable": false,
32-
"component": "shared.button"
54+
"pluginOptions": {
55+
"i18n": {
56+
"localized": true
57+
}
58+
},
59+
"component": "shared.button",
60+
"repeatable": false
3361
},
3462
"perks": {
35-
"displayName": "Perks",
3663
"type": "component",
37-
"repeatable": true,
38-
"component": "shared.perks"
64+
"pluginOptions": {
65+
"i18n": {
66+
"localized": true
67+
}
68+
},
69+
"component": "shared.perks",
70+
"repeatable": true
3971
},
4072
"additional_perks": {
41-
"displayName": "Additional Perks",
4273
"type": "component",
43-
"repeatable": true,
44-
"component": "shared.perks"
74+
"pluginOptions": {
75+
"i18n": {
76+
"localized": true
77+
}
78+
},
79+
"component": "shared.perks",
80+
"repeatable": true
4581
},
4682
"product": {
4783
"type": "relation",

strapi/types/generated/contentTypes.d.ts

Lines changed: 100 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -800,22 +800,62 @@ export interface ApiPlanPlan extends Struct.CollectionTypeSchema {
800800
options: {
801801
draftAndPublish: true;
802802
};
803+
pluginOptions: {
804+
i18n: {
805+
localized: true;
806+
};
807+
};
803808
attributes: {
804-
additional_perks: Schema.Attribute.Component<'shared.perks', true>;
809+
additional_perks: Schema.Attribute.Component<'shared.perks', true> &
810+
Schema.Attribute.SetPluginOptions<{
811+
i18n: {
812+
localized: true;
813+
};
814+
}>;
805815
createdAt: Schema.Attribute.DateTime;
806816
createdBy: Schema.Attribute.Relation<'oneToOne', 'admin::user'> &
807817
Schema.Attribute.Private;
808-
CTA: Schema.Attribute.Component<'shared.button', false>;
809-
featured: Schema.Attribute.Boolean & Schema.Attribute.DefaultTo<false>;
810-
locale: Schema.Attribute.String & Schema.Attribute.Private;
811-
localizations: Schema.Attribute.Relation<'oneToMany', 'api::plan.plan'> &
812-
Schema.Attribute.Private;
813-
name: Schema.Attribute.String;
814-
perks: Schema.Attribute.Component<'shared.perks', true>;
815-
price: Schema.Attribute.Integer;
818+
CTA: Schema.Attribute.Component<'shared.button', false> &
819+
Schema.Attribute.SetPluginOptions<{
820+
i18n: {
821+
localized: true;
822+
};
823+
}>;
824+
featured: Schema.Attribute.Boolean &
825+
Schema.Attribute.SetPluginOptions<{
826+
i18n: {
827+
localized: true;
828+
};
829+
}> &
830+
Schema.Attribute.DefaultTo<false>;
831+
locale: Schema.Attribute.String;
832+
localizations: Schema.Attribute.Relation<'oneToMany', 'api::plan.plan'>;
833+
name: Schema.Attribute.String &
834+
Schema.Attribute.SetPluginOptions<{
835+
i18n: {
836+
localized: true;
837+
};
838+
}>;
839+
perks: Schema.Attribute.Component<'shared.perks', true> &
840+
Schema.Attribute.SetPluginOptions<{
841+
i18n: {
842+
localized: true;
843+
};
844+
}>;
845+
price: Schema.Attribute.Integer &
846+
Schema.Attribute.SetPluginOptions<{
847+
i18n: {
848+
localized: true;
849+
};
850+
}>;
816851
product: Schema.Attribute.Relation<'manyToOne', 'api::product.product'>;
817852
publishedAt: Schema.Attribute.DateTime;
818-
sub_text: Schema.Attribute.String;
853+
sub_text: Schema.Attribute.String &
854+
Schema.Attribute.SetPluginOptions<{
855+
i18n: {
856+
localized: true;
857+
};
858+
}>;
819859
updatedAt: Schema.Attribute.DateTime;
820860
updatedBy: Schema.Attribute.Relation<'oneToOne', 'admin::user'> &
821861
Schema.Attribute.Private;
@@ -903,6 +943,11 @@ export interface ApiProductProduct extends Struct.CollectionTypeSchema {
903943
options: {
904944
draftAndPublish: true;
905945
};
946+
pluginOptions: {
947+
i18n: {
948+
localized: true;
949+
};
950+
};
906951
attributes: {
907952
categories: Schema.Attribute.Relation<
908953
'oneToMany',
@@ -911,22 +956,57 @@ export interface ApiProductProduct extends Struct.CollectionTypeSchema {
911956
createdAt: Schema.Attribute.DateTime;
912957
createdBy: Schema.Attribute.Relation<'oneToOne', 'admin::user'> &
913958
Schema.Attribute.Private;
914-
description: Schema.Attribute.String;
959+
description: Schema.Attribute.String &
960+
Schema.Attribute.SetPluginOptions<{
961+
i18n: {
962+
localized: true;
963+
};
964+
}>;
915965
dynamic_zone: Schema.Attribute.DynamicZone<
916966
['dynamic-zone.related-products', 'dynamic-zone.cta']
917-
>;
918-
featured: Schema.Attribute.Boolean & Schema.Attribute.DefaultTo<false>;
919-
images: Schema.Attribute.Media<'images' | 'files' | 'videos', true>;
920-
locale: Schema.Attribute.String & Schema.Attribute.Private;
967+
> &
968+
Schema.Attribute.SetPluginOptions<{
969+
i18n: {
970+
localized: true;
971+
};
972+
}>;
973+
featured: Schema.Attribute.Boolean &
974+
Schema.Attribute.SetPluginOptions<{
975+
i18n: {
976+
localized: true;
977+
};
978+
}> &
979+
Schema.Attribute.DefaultTo<false>;
980+
images: Schema.Attribute.Media<'images' | 'files' | 'videos', true> &
981+
Schema.Attribute.SetPluginOptions<{
982+
i18n: {
983+
localized: true;
984+
};
985+
}>;
986+
locale: Schema.Attribute.String;
921987
localizations: Schema.Attribute.Relation<
922988
'oneToMany',
923989
'api::product.product'
924-
> &
925-
Schema.Attribute.Private;
926-
name: Schema.Attribute.String;
927-
perks: Schema.Attribute.Component<'shared.perks', true>;
990+
>;
991+
name: Schema.Attribute.String &
992+
Schema.Attribute.SetPluginOptions<{
993+
i18n: {
994+
localized: true;
995+
};
996+
}>;
997+
perks: Schema.Attribute.Component<'shared.perks', true> &
998+
Schema.Attribute.SetPluginOptions<{
999+
i18n: {
1000+
localized: true;
1001+
};
1002+
}>;
9281003
plans: Schema.Attribute.Relation<'oneToMany', 'api::plan.plan'>;
929-
price: Schema.Attribute.Integer;
1004+
price: Schema.Attribute.Integer &
1005+
Schema.Attribute.SetPluginOptions<{
1006+
i18n: {
1007+
localized: true;
1008+
};
1009+
}>;
9301010
publishedAt: Schema.Attribute.DateTime;
9311011
slug: Schema.Attribute.UID<'name'>;
9321012
updatedAt: Schema.Attribute.DateTime;

0 commit comments

Comments
 (0)