Skip to content

Commit f34e070

Browse files
authored
Merge pull request #83 from strapi/feat/72-middleware-populate
feat: Route Middleware to Populate Data
2 parents 584421e + 498398e commit f34e070

File tree

17 files changed

+614
-126
lines changed

17 files changed

+614
-126
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ This installs dependencies, builds your project, and starts your server. You’r
104104

105105
## Customization
106106

107-
- The Strapi application contains a custom population middleware in order to populate more data than what it is set by default. You can find it in the `./strapi/src/middlewares/deepPopulate.ts` file.
107+
- The Strapi application contains a custom population middlewares in every api route.
108108

109109
- The Strapi application contains a postinstall script that will regenerate an uuid for the project in order to get some anonymous usage information concerning this demo. You can disable it by removing the uuid inside the `./strapi/packages.json` file.
110110

next/components/products/featured.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export const Featured = ({
1212
products: Product[];
1313
locale: string;
1414
}) => {
15+
const [firstProduct, secondProduct, thirdProduct] = products;
16+
1517
return (
1618
<div className="py-20">
1719
<h2 className="text-2xl md:text-4xl font-medium bg-clip-text text-transparent bg-gradient-to-b from-neutral-800 via-white to-white mb-2">
@@ -22,11 +24,17 @@ export const Featured = ({
2224
</p>
2325
<div className="grid grid-cols-1 md:grid-cols-3 gap-10">
2426
<div className="md:col-span-2">
25-
<FeaturedItem product={products[0]} locale={locale} />
27+
{firstProduct ? (
28+
<FeaturedItem product={firstProduct} locale={locale} />
29+
) : null}
2630
</div>
2731
<div className="grid gap-10">
28-
<FeaturedItem product={products[1]} locale={locale} />
29-
<FeaturedItem product={products[2]} locale={locale} />
32+
{secondProduct ? (
33+
<FeaturedItem product={secondProduct} locale={locale} />
34+
) : null}
35+
{thirdProduct ? (
36+
<FeaturedItem product={thirdProduct} locale={locale} />
37+
) : null}
3038
</div>
3139
</div>
3240
<div className="grid grid-cols-1 md:grid-cols-3 gap-10"></div>

strapi/config/middlewares.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ export default [
99
'strapi::session',
1010
'strapi::favicon',
1111
'strapi::public',
12-
'global::deepPopulate',
1312
];
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* `article-populate` middleware
3+
*/
4+
import type { Core } from '@strapi/strapi';
5+
6+
const populate = {
7+
image: true,
8+
categories: {
9+
populate: {
10+
product: true,
11+
articles: {
12+
populate: {
13+
dynamic_zone: {
14+
on: {
15+
'dynamic-zone.related-articles': true,
16+
'dynamic-zone.cta': {
17+
populate: {
18+
CTAs: true,
19+
},
20+
},
21+
},
22+
},
23+
},
24+
},
25+
},
26+
},
27+
dynamic_zone: {
28+
on: {
29+
'dynamic-zone.related-articles': {
30+
populate: {
31+
articles: {
32+
populate: {
33+
image: true,
34+
},
35+
},
36+
},
37+
},
38+
'dynamic-zone.cta': {
39+
populate: {
40+
CTAs: true,
41+
},
42+
},
43+
},
44+
},
45+
seo: {
46+
populate: {
47+
metaImage: true,
48+
},
49+
},
50+
};
51+
52+
export default (config, { strapi }: { strapi: Core.Strapi }) => {
53+
return async (ctx, next) => {
54+
ctx.query.populate = populate;
55+
await next();
56+
};
57+
};

strapi/src/api/article/routes/article.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,13 @@
33
*/
44
import { factories } from '@strapi/strapi';
55

6-
export default factories.createCoreRouter('api::article.article');
6+
export default factories.createCoreRouter('api::article.article', {
7+
config: {
8+
find: {
9+
middlewares: ['api::article.article-populate'],
10+
},
11+
findOne: {
12+
middlewares: ['api::article.article-populate'],
13+
},
14+
},
15+
});
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/**
2+
* `blog-page-populate` middleware
3+
*/
4+
import type { Core } from '@strapi/strapi';
5+
6+
const populate = {
7+
dynamic_zone: {
8+
on: {
9+
'dynamic-zone.hero': {
10+
populate: {
11+
CTAs: true,
12+
},
13+
},
14+
'dynamic-zone.features': {
15+
populate: {
16+
globe_card: true,
17+
ray_card: {
18+
populate: {
19+
before_ray_items: true,
20+
after_ray_items: true,
21+
},
22+
},
23+
graph_card: {
24+
populate: {
25+
top_items: true,
26+
},
27+
},
28+
social_media_card: {
29+
populate: {
30+
logos: {
31+
populate: {
32+
image: true,
33+
},
34+
},
35+
},
36+
},
37+
},
38+
},
39+
'dynamic-zone.testimonials': {
40+
populate: {
41+
testimonials: {
42+
populate: {
43+
user: {
44+
populate: {
45+
image: true,
46+
},
47+
},
48+
},
49+
},
50+
},
51+
},
52+
'dynamic-zone.how-it-works': {
53+
populate: {
54+
steps: true,
55+
},
56+
},
57+
'dynamic-zone.brands': {
58+
populate: {
59+
logos: {
60+
populate: {
61+
image: true,
62+
},
63+
},
64+
},
65+
},
66+
'dynamic-zone.pricing': {
67+
populate: {
68+
plans: {
69+
populate: {
70+
perks: true,
71+
additional_perks: true,
72+
CTA: true,
73+
product: true,
74+
},
75+
},
76+
},
77+
},
78+
'dynamic-zone.launches': {
79+
populate: {
80+
launches: true,
81+
},
82+
},
83+
'dynamic-zone.cta': {
84+
populate: {
85+
CTAs: true,
86+
},
87+
},
88+
'dynamic-zone.faq': {
89+
populate: {
90+
faqs: true,
91+
},
92+
},
93+
'dynamic-zone.form-next-to-section': {
94+
populate: {
95+
form: {
96+
populate: {
97+
inputs: true,
98+
},
99+
},
100+
section: {
101+
populate: {
102+
users: {
103+
populate: {
104+
image: true,
105+
},
106+
},
107+
},
108+
},
109+
social_media_icon_links: {
110+
populate: {
111+
image: true,
112+
link: true,
113+
},
114+
},
115+
},
116+
},
117+
'dynamic-zone.related-articles': true,
118+
'dynamic-zone.related-products': true,
119+
},
120+
},
121+
seo: {
122+
populate: {
123+
metaImage: true,
124+
},
125+
},
126+
};
127+
128+
export default (config, { strapi }: { strapi: Core.Strapi }) => {
129+
return async (ctx, next) => {
130+
ctx.query.populate = populate;
131+
await next();
132+
};
133+
};

strapi/src/api/blog-page/routes/blog-page.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@
33
*/
44
import { factories } from '@strapi/strapi';
55

6-
export default factories.createCoreRouter('api::blog-page.blog-page');
6+
export default factories.createCoreRouter('api::blog-page.blog-page', {
7+
config: {
8+
find: {
9+
middlewares: ['api::blog-page.blog-page-populate'],
10+
},
11+
},
12+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* `global-populate` middleware
3+
*/
4+
import type { Core } from '@strapi/strapi';
5+
6+
const populate = {
7+
navbar: {
8+
populate: {
9+
left_navbar_items: true,
10+
right_navbar_items: true,
11+
logo: {
12+
populate: {
13+
image: true,
14+
},
15+
},
16+
},
17+
},
18+
footer: {
19+
populate: {
20+
internal_links: true,
21+
policy_links: true,
22+
social_media_links: true,
23+
logo: {
24+
populate: {
25+
image: true,
26+
},
27+
},
28+
},
29+
},
30+
seo: {
31+
populate: {
32+
metaImage: true,
33+
},
34+
},
35+
};
36+
37+
export default (config, { strapi }: { strapi: Core.Strapi }) => {
38+
return async (ctx, next) => {
39+
ctx.query.populate = populate;
40+
await next();
41+
};
42+
};

strapi/src/api/global/routes/global.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@
33
*/
44
import { factories } from '@strapi/strapi';
55

6-
export default factories.createCoreRouter('api::global.global');
6+
export default factories.createCoreRouter('api::global.global', {
7+
config: {
8+
find: {
9+
middlewares: ['api::global.global-populate'],
10+
},
11+
},
12+
});

0 commit comments

Comments
 (0)