0

I am building a website with an articles page, and on the home page we have a featured article (The most recent one). When we upload a new article, we would like the one on the home page to update automatically.

So what I need to do, is use a JS function on page load, to then search the articles file for the article with the most recent date, and pull the data from this element and use it on the home page.

LondonMassive
  • 367
  • 2
  • 5
  • 20

2 Answers2

1

Are you going to have lots of articles published ? What is the frequency ?

In any of the case if you build simple API for retrieving articles then you can achieve your goal using below.

  1. create a lighweight API to give you latest details about articles for e.g. HEAD /{category}/articles this might return latest articles ID.
  2. Compare from step1 what articles Id you have retrieved before.
  3. If from first step you get latest articles Id then fire another API to get article data using /{category}/articles/{articleId_retrieved_from_step2}

you can perform above steps in standard frequency based on your need using standard AJAX or simple polling frameworks. Refer this for more details https://javascript.info/long-polling

omkarjoshi
  • 86
  • 10
0

I will recommend to use SSE (Server Sent Event) which work without page refresh and continuous check the latest data in the database.
Please check the below link
https://www.w3schools.com/html/html5_serversentevents.asp

Vishal Petkar
  • 369
  • 1
  • 4
  • 20