Strapi allows customization to a great extent with the help of local plugins.
These plugins can be simple React applications.
Generated plugins reside within /plugins
folder.
In this post, I am creating a local plugin posts
which displays posts from JSONPlaceholder Fake API.
Here is the link of the repository for reference.
Create new strapi project by running the following command in terminal:
yarn create strapi-app project-name --quickstart
cd project-name
Generate local plugin you want to build using the command:
yarn strapi generate:plugin posts
New folder /posts
is created in /plugins
directory.
To see this plugin in admin panel, you should run:
yarn build
yarn develop
Now we can see the plugin link in Strapi
This is the directory structure of the generated plugin
posts/
└─── admin/ # Contains the plugin's front-end
| └─── src/ # Source code directory
| └─── index.js # Entry point of the plugin
| └─── pluginId.js # Name of the plugin
| |
| └─── components/ # Contains the list of React components used by the plugin
| └─── containers/
| | └─── App/ # Container used by every others containers
| | └─── Initializer/ # This container is required to execute the logic soon after the plugin is mounted.
| └─── translations/ # Contains the translations to make the plugin internationalized
| └─── en.json
| └─── index.js # File that exports all the plugin's translations.
| └─── fr.json
└─── config/ # Contains the configurations of the plugin
| └─── functions/
| | └─── bootstrap.js # Asynchronous bootstrap function that runs before the app gets started
| └─── policies/ # Folder containing the plugin's policies
| └─── queries/ # Folder containing the plugin's models queries
| └─── routes.json # Contains the plugin's API routes
└─── controllers/ # Contains the plugin's API controllers
└─── middlewares/ # Contains the plugin's middlewares
└─── models/ # Contains the plugin's API models
└─── services/ # Contains the plugin's API services
To enable local plugin development run the following command:
yarn develop --watch-admin
We can delete translations
folder, if not necessary. We need to set default value of trads
to {}
in the menu object provided
to the plugin, if deleted.
By default strapi provides you with /posts
("path": "/"
) route.
Call third party api from strapi route instead of directly calling it from the components.
Retreive back-end URL using strapi.backendURL
API to call /posts
route.
We can customize the response from index
function which is the handler specified for /posts
route.
Call third party api to fetch posts here.
Routing is implemented in App
component at /posts/admin/src/conatainers/App/index.js
I am using Buffet.js which is a React Components library used by Strapi to display posts in <Table />
This is how it looks!
Accessing Environment variable in Frontend
Environmental variables in strapi frontend cannot be accessed directly with process.env.VARIABLE
.
Instead we should set it by customizing webpack config
Create file at project/admin/admin.config.js
Access it within components:
Happy Learning